#!/usr/bin/python3
# -*- coding:utf-8 -*-
# @Time   :2021/12/8
# @Author :CJX
# @File   :main.py
import os
import sys
from typing import List, Union, Any
import RPi.GPIO as GPIO
import can
import time
import numpy as np
from smbus import SMBus
from Injection_valve_runze import injection_port
from Injection_valve_runze import insert_database
from dox_enquiry import dox_command
from PH_controller import PH_choice
from PH_controller import PH_controller
from PH_controller import PH_status
import dht11
from threading import Thread
import threading
import pymysql

import binascii
import importlib
import codecs
import serial
# DHT20
from DHT22 import *

importlib.reload(sys)
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.cleanup()
ser = serial.Serial('/dev/ttySC0', 9600)


def DHT11():
    Tem1H = 49
    HumH = 80
    global err_inquiry_21, err_inquiry_1_4
    global DHT11_state_1, DHT11_state_2, DHT11_state_3, DHT11_state_4, DHT11_state_21
    DHT20_num = DHT22()
    temperature = int(float('%-3.1f' % DHT20_num[0]))
    humidity = int(float('%-3.1f' % DHT20_num[1]))
    print("内部温度是： ", temperature, "内部湿度是： ", humidity)
    serial_val_modify("homepage.tem.val=%s" % temperature)
    serial_val_modify("homepage.hum.val=%s" % humidity)
    if temperature >= Tem1H:
        Tem1H_t = 1
        DHT11_state_21 = [21]
        dox_command([21], err_inquiry_21)
        time1 = time.strftime('%H:%M:%S', time.localtime(time.time()))
        # serial_val_modify("warningpage.data0.insert(\"%s^环境温度高^无\")" % time1)  # 存入故障信息
        print("开启")
    elif temperature <= (Tem1H - 3):
        Tem1H_t = 0
        DHT11_state_21 = [0]
        time1 = time.strftime('%H:%M:%S', time.localtime(time.time()))
        # serial_val_modify("warningpage.data0.insert(\"%s^环境温度正常^无\")" % time1)  # 存入故障信息
        dox_command([0], err_inquiry_21)
    if humidity >= HumH:
        HumH_t = 1
        DHT11_state_21 = [21]
        dox_command([21], err_inquiry_21)
        time1 = time.strftime('%H:%M:%S', time.localtime(time.time()))
        # serial_val_modify("warningpage.data0.insert(\"%s^环境湿度高^无\")" % time1)  # 存入故障信息
    elif humidity <= (HumH - 2):
        HumH_t = 0
        DHT11_state_21 = [0]
        dox_command([0], err_inquiry_21)
        time1 = time.strftime('%H:%M:%S', time.localtime(time.time()))
        # serial_val_modify("warningpage.data0.insert(\"%s^环境湿度正常^无\")" % time1)  # 存入故障信息


def water_level_monitoring():
    global i2c40, i2c41, i2c42, i2c43
    global YW40L, YW41L, YW42L, YW43L, YW40H, YW41H, YW42H, YW43H
    global i2c40_t, i2c41_t, i2c42_t, i2c43_t, err_inquiry_14, err_inquiry_15, err_inquiry_16
    time.sleep(2)
    # 当设置为PUD_OFF时表示没有上拉电阻和下拉电阻，当设置为PUD_DOWN 时为下拉电阻，设置为PUD_UP是为上拉电阻。
    GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_UP)  # 清洗液高液位
    GPIO.setup(6, GPIO.IN, pull_up_down=GPIO.PUD_UP)  # 清洗液低液位
    GPIO.setup(13, GPIO.IN, pull_up_down=GPIO.PUD_UP)  # 保护液高液位
    GPIO.setup(19, GPIO.IN, pull_up_down=GPIO.PUD_UP)  # 保护液低液位
    GPIO.setup(26, GPIO.IN, pull_up_down=GPIO.PUD_UP)  # 缓冲液6高液位
    GPIO.setup(12, GPIO.IN, pull_up_down=GPIO.PUD_UP)  # 缓冲液6低液位
    GPIO.setup(16, GPIO.IN, pull_up_down=GPIO.PUD_UP)  # 缓冲液4高液位
    GPIO.setup(20, GPIO.IN, pull_up_down=GPIO.PUD_UP)  # 缓冲液4低液位
    while 1:
        time.sleep(0.5)

        if GPIO.input(5) == 0 and GPIO.input(6) == 1:
            i2c40 = 100
        elif GPIO.input(5) == 0 and GPIO.input(6) == 0:
            i2c40 = 400
            # print("清洗液高液位")
        elif GPIO.input(6) == 1 and GPIO.input(5) == 1:
            i2c40 = 0
            # print("清洗液低液位")

        if GPIO.input(13) == 0 and GPIO.input(19) == 1:
            i2c41 = 100
        elif GPIO.input(19) == 0 and GPIO.input(13) == 0:
            i2c41 = 400
            # print("保护液高液位")
        elif GPIO.input(13) == 1 and GPIO.input(19) == 1:
            i2c41 = 0
            # print("保护液低液位")

        if GPIO.input(26) == 0 and GPIO.input(12) == 1:
            i2c43 = 100
        elif GPIO.input(26) == 1 and GPIO.input(12) == 1:
            i2c43 = 0
            # print("缓冲液6低液位")
        elif GPIO.input(12) == 0 and GPIO.input(26) == 0:
            i2c43 = 400
            # print("缓冲液6高液位")

        if GPIO.input(16) == 0 and GPIO.input(20) == 1:
            i2c42 = 100
        elif GPIO.input(16) == 1 and GPIO.input(20) == 1:
            i2c42 = 0
            # print("缓冲液4低液位")
        elif GPIO.input(20) == 0 and GPIO.input(16) == 0:
            i2c42 = 400
            # print("缓冲液4高液位")

        # iic = SMBus(1)  # 创建接口
        # i2c40_t = 0
        # i2c41_t = 0
        # i2c42_t = 0
        # i2c43_t = 0
        # while 1:
        #     list1, list2, list3, list4 = [], [], [], []
        #     for i in range(5):  # 初始化，预读n组数据
        #         data1 = iic.read_word_data(0x40, 0x00)
        #         list1.append(data1)
        #         data2 = iic.read_word_data(0x41, 0x00)
        #         list2.append(data2)
        #         data3 = iic.read_word_data(0x42, 0x00)
        #         list3.append(data3)
        #         data4 = iic.read_word_data(0x43, 0x00)
        #         list4.append(data4)
        #         time.sleep(0.1)
        #
        #     average1 = round(sum(list1) / len(list1))  # 平均值四舍五入
        #     average2 = round(sum(list2) / len(list2))
        #     average3 = round(sum(list3) / len(list3))
        #     average4 = round(sum(list4) / len(list4))
        #     i2c40 = average1
        #     i2c41 = average2
        #     i2c42 = average3
        #     i2c43 = average4
        if i2c40 <= YW40L and i2c40_t == 0:
            i2c40_t = 1
            # # serial_val_modify("page0.now_position.txt=\"清洗液液位过低\"")
            time1 = time.strftime('%H:%M:%S', time.localtime(time.time()))
            serial_val_modify("warningpage.data0.insert(\"%s^清洗液液位过低\")" % time1)  # 存入故障信息
            dox_command([14], err_inquiry_14)
        if i2c40 > YW40L and i2c40_t == 1:
            i2c40_t = 0
            # # serial_val_modify("page0.now_position.txt=\"清洗液液位正常\"")
            dox_command([0], err_inquiry_14)

        if i2c41 <= YW41L and i2c41_t == 0:
            dox_command([15], err_inquiry_15)
            i2c41_t = 1
            # # serial_val_modify("page0.now_position.txt=\"保护液液位过低\"")
            time1 = time.strftime('%H:%M:%S', time.localtime(time.time()))
            serial_val_modify("warningpage.data0.insert(\"%s^保护液液位过低\")" % time1)  # 存入故障信息
        if i2c41 > YW41L and i2c41_t == 1:
            dox_command([0], err_inquiry_15)
            i2c41_t = 0
            # # serial_val_modify("page0.now_position.txt=\"保护液液位正常\"")

        if i2c42 <= YW42L and i2c42_t == 0:
            dox_command([16], err_inquiry_16)
            i2c42_t = 1
            # # serial_val_modify("page0.now_position.txt=\"缓冲液4液位过低\"")
            time1 = time.strftime('%H:%M:%S', time.localtime(time.time()))
            serial_val_modify("warningpage.data0.insert(\"%s^缓冲液4液位过低\")" % time1)  # 存入故障信息
        if i2c42 > YW42L and i2c42_t == 1 and i2c43 > YW43L:
            dox_command([0], err_inquiry_16)
            i2c42_t = 0
            # # serial_val_modify("page0.now_position.txt=\"缓冲液4液位正常\"")

        if i2c43 <= YW43L and i2c43_t == 0:
            dox_command([16], err_inquiry_16)
            i2c43_t = 1
            # # serial_val_modify("page0.now_position.txt=\"缓冲液6液位过低\"")
            time1 = time.strftime('%H:%M:%S', time.localtime(time.time()))
            serial_val_modify("warningpage.data0.insert(\"%s^缓冲液6液位过低\")" % time1)  # 存入故障信息
        if i2c43 > YW43L and i2c43_t == 1 and i2c42 > YW42L:
            dox_command([0], err_inquiry_16)
            i2c43_t = 0
            # # serial_val_modify("page0.now_position.txt=\"缓冲液6液位正常\"")
    #     time.sleep(0.2)
    #     print("i2c40:" + str(i2c40))
    #     print("i2c41:" + str(i2c41))
    #     print("i2c42:" + str(i2c42))
    #     print("i2c43:" + str(i2c43))


def sql_insert_mode(num):  # 储存当前所处模式，在重启后可以回到重启前状态
    con = pymysql.connect(
        host='127.0.0.1',
        port=3306,
        user='root',
        password='!qAz2WsX3edc',
        db='ph',
        charset='utf8'
    )
    cur = con.cursor()
    mode_state = num
    time1 = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
    cur.executemany("INSERT mode(mode,time) VALUE(%s,%s)", [(mode_state, time1)])

    cur = con.cursor()
    cur.execute("SELECT min(id) FROM mode ")
    min_mode = int(cur.fetchone()[0])
    cur.execute("SELECT max(id) FROM mode ")
    max_mode = int(cur.fetchone()[0])
    if (max_mode - min_mode) >= 1000:
        sql2 = ("delete FROM mode " + "WHERE id <= %s " % (max_mode - 100) + " and id >= %s" % min_mode)
        cur.execute(sql2)
    con.commit()
    cur.close()
    return


def query_mode():  # 查询当前所处模式
    global mode
    con = pymysql.connect(
        host='127.0.0.1',
        port=3306,
        user='root',
        password='!qAz2WsX3edc',
        db='ph',
        charset='utf8'
    )
    cur = con.cursor()
    cur.execute("select mode from mode where id =(SELECT max(id) FROM mode)")
    mode_now = int(cur.fetchone()[0])
    if mode_now == 1:
        #  #  # serial_val_modify("page0.now_status.txt=\"自动模式\"")
        serial_val_modify("homepage.mod_sw.val=1")
    elif mode_now == 0:
        #  #  # serial_val_modify("page0.now_status.txt=\"手动模式\"")
        serial_val_modify("homepage.mod_sw.val=0")
    mode = mode_now


def restart_exe():  # 程序重启函数，用于自动转手动过程中重启，加速反馈
    python = sys.executable
    os.execl(python, python, *sys.argv)


def injection_port_0():
    injection_port(0)  # 切换阀复位,X-0指令
    time.sleep(5)
    if insert_database() == "b001":  # 查询指令
        # # serial_val_modify("page0.now_position.txt=\"切换阀初始化位置\"")
        return  # 成功
    else:
        time.sleep(3)
        if insert_database() == "b001":  # 查询指令
            # # serial_val_modify("page0.now_position.txt=\"切换阀初始化位置\"")
            return  # 成功
        else:
            time1 = time.strftime('%H:%M:%S', time.localtime(time.time()))
            serial_val_modify("warningpage.data0.insert(\"%s^切换阀故障\")" % time1)  # 存入故障信息
            # # serial_val_modify("page0.now_position.txt=\"切换阀故障\"")
            return


def recv():  # 接收串口信息
    while True:
        data = ser.read_all()
        if data == b'':
            continue
        else:
            break
    return data


def serial_read():  # 接收串口信息
    promptly = 0  # 用于快速判断串口信息，并做出反应
    while 1:
        global serial_txt, mode, serial_time
        data = recv()
        # 转换为十六进制字节流
        s_hex = binascii.hexlify(data)
        # 字节流转换成字符串
        s_hex = bytes.decode(s_hex)
        serial_txt = s_hex[0:6]
        serial_time = s_hex[0:8]
        if promptly != s_hex:  # 串口信息有变动就做出反应
            promptly = s_hex
            if promptly[0:6] == "ffff00":
                mode = 1  # 开启远程模式
                print("远程")
                sql_insert_mode(1)

            if promptly[0:6] == "ffff01":
                mode = 0  # 开启就地模式
                print("就地")
                sql_insert_mode(0)


def serial_val_modify(val):  # 发送串口信息
    s = val
    # 字符串变gbk字符型字节流
    s_byte = s.encode("gbk")
    # 字符型字节流转十六进制字节流, 与b2a_hex()，bytes.hex()方法功能一样
    s_hex = binascii.hexlify(s_byte)
    s_hex = s_hex + b'FFFFFF'
    # 十六进制字节流解码
    s_gbk = codecs.decode(s_hex, 'hex')
    ser.write(s_gbk)


def can_open():  # 开启can通讯
    os.system('sudo ip link set can0 type can bitrate 250000')
    os.system('sudo ifconfig can0 up')


def io_restart():  # 重启can通讯
    global status_restart_timing
    status_restart_timing = 1
    while status_restart_timing == 1:  # 用于在io查询时其他程序不输送
        time.sleep(0.01)


def io_status():  # 重启can通讯
    while 1:
        # io_status_quit用于判断轮询是否退出，status_restart_timing用于判断是否重启can通讯
        global status_restart_timing, io_status_quit
        time.sleep(0.1)
        if status_restart_timing == 1 and io_status_quit == 1:
            status_restart_timing = 0
            print("查询dido状态重启")
            io()


def io():
    bus = can.Bus(interface='socketcan', channel='can0', receive_own_messages=eval('False'))
    while 1:
        global var1, var2, data_do, data_di  # 类型为list
        global status_restart_timing, io_status_quit  # 用于重启can通讯
        global do_location, di_location
        # 用于各种复位
        io_status_quit = 0
        if status_restart_timing == 1:
            print("查询dido状态关闭")
            os.system('sudo ifconfig can0 down')
            can_open()
            io_status_quit = 1
            break
        # global edge, l_s_time  # 长按和短按判断，类型为array元素为True/False
        inquiry_do = can.Message(arbitration_id=0x0207, data=[0x01], is_extended_id=False)  # 问询
        bus.send(inquiry_do)  # 0x0201 DO状态 返回513
        message_do = bus.recv(timeout=1)  # 接收数据
        type_id_do = message_do.arbitration_id  # 返回can报文类型
        if type_id_do == 519:
            # time_float1 = message_do.timestamp          # 浮点数时间戳
            # time_date1 = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(float(message_do.timestamp)))  # 转str时间
            # 取回4个字节,预留32通道,bytearray->int->bin(str)->去'0b'->bin->左补32位->反向排列
            data_do = '{:0>32b}'.format(int(bin(int.from_bytes(message_do.data[0:4], "little")).replace('0b', ''), 2))[
                      ::-1]
            # print(data_do, type(data_do))
            if var1 != data_do:  # 差异检测，差异写入数据库
                # write_sql(time_date1, type_id_do, data_do)+++++++++++注意排列顺序有修改
                var1 = data_do  # 给中间值赋新值
                do_location = list(data_do)
        time.sleep(0.1)
        inquiry_di = can.Message(arbitration_id=0x0308, data=[0x01], is_extended_id=False)
        bus.send(inquiry_di)  # 0x0301  返回769/0x0301 di状态
        message_di = bus.recv(timeout=1)  # 接收数据
        type_id_di = message_di.arbitration_id  # 返回can报文类型
        if type_id_di == 776:
            # 取回4个字,预留32通道,bytearray->int->bin(str)->去'0b'->bin->左补32位->反向排列
            data_di = '{:0>32b}'.format(int(bin(int.from_bytes(message_di.data[0:4], "little")).replace('0b', ''), 2))[
                      ::-1]
            # print(data_di, type(data_di))
            if data_di != var2:  # 差异检测，差异写入数据库
                # write_sql(time_date2, type_id_di, data_di)
                # for k in range(32):
                #     if var2[k] == '0' and data_di[k] == '1':  # 32点上升沿检测,没有检测到不复位
                #         edge[0, k] = 1  # 写入边缘检测edge[0]
                #         edge[1, k] = 0  # 复位下降沿
                #         # time_diff[0, k] = time.time()  # 把时间写入array[0]，上升沿触发时间戳
                #     if var2[k] == '1' and data_di[k] == '0':  # 32点下降沿检测,没有检测到不复位
                #         edge[1, k] = 1  # 写入边缘检测edge[1]
                #         edge[0, k] = 0  # 复位上升沿
                #         # time_diff[1, k] = time.time()  # 把时间写入array[1]，下降沿触发时间戳
                #     # l_s_time[0, k] = 0 < (time_diff[1, k] - time_diff[0, k]) < 3  # 短脉冲判断
                #     # l_s_time[1, k] = 3 < (time_diff[1, k] - time_diff[0, k]) < 10  # 长脉冲判断
                #     # print((time_diff[1, k] - time_diff[0, k]))
                var2 = data_di  # 给中间值赋新值
                di_location = list(data_di)  # 字符串转列表输出
        time.sleep(0.1)


def err_inquiry_function(err_num):  # 供dox_command函数使用，使某些do不复位
    if do_location[err_num - 1:err_num][0] == "1":
        err_inquiry_num = [err_num]
    else:
        err_inquiry_num = [0]
    return err_inquiry_num


def do_not_reset():
    while 1:
        time.sleep(0.2)
        global err_inquiry_s1, err_inquiry_s2, err_inquiry_s3, err_inquiry_s4_s10, err_inquiry_s01, err_inquiry_s14
        global err_inquiry_s14_13, err_inquiry_s15, err_inquiry_14, err_inquiry_15, err_inquiry_16
        global err_inquiry_21, err_inquiry_1_4, err_inquiry_27_30, err_inquiry_s3_exit, err_inquiry_s10_s13
        global s4_state, s5_state, s6_state, s7_state, s8_state
        global s9_state, s10_state, s11_state, s12_state, s13_state, s1_state, s2_state
        global DHT11_state_1, DHT11_state_2, DHT11_state_3, DHT11_state_4, DHT11_state_21

        err_inquiry_all = \
            DHT11_state_1 + DHT11_state_2 + \
            DHT11_state_3 + DHT11_state_4 + err_inquiry_function(11) + \
            err_inquiry_function(12) + s2_state_13 + err_inquiry_function(14) + \
            err_inquiry_function(15) + err_inquiry_function(16) + s5_state + \
            DHT11_state_21 + s4_state + s2_state + \
            s1_state + s13_state + s9_state + \
            s12_state + s8_state + s11_state + \
            s7_state + s10_state + s6_state
        # 用于s1
        err_inquiry_s1 = \
            DHT11_state_1 + DHT11_state_2 + \
            DHT11_state_3 + DHT11_state_4 + err_inquiry_function(11) + \
            err_inquiry_function(12) + err_inquiry_function(14) + \
            err_inquiry_function(15) + err_inquiry_function(16) + s5_state + \
            DHT11_state_21 + s4_state + s2_state + \
            s13_state + s9_state + \
            s12_state + s8_state + s11_state + \
            s7_state + s10_state + s6_state
        # 用于s1
        err_inquiry_s2 = \
            DHT11_state_1 + DHT11_state_2 + \
            DHT11_state_3 + DHT11_state_4 + err_inquiry_function(11) + \
            err_inquiry_function(12) + err_inquiry_function(14) + \
            err_inquiry_function(15) + err_inquiry_function(16) + s5_state + \
            DHT11_state_21 + s4_state + \
            s1_state + s13_state + s9_state + \
            s12_state + s8_state + s11_state + \
            s7_state + s10_state + s6_state
        # 用于s2
        err_inquiry_s3 = \
            DHT11_state_1 + DHT11_state_2 + \
            DHT11_state_3 + DHT11_state_4 + err_inquiry_function(11) + \
            err_inquiry_function(12) + err_inquiry_function(14) + \
            err_inquiry_function(15) + err_inquiry_function(16) + \
            DHT11_state_21 + \
            s1_state + s13_state + s9_state + \
            s12_state + s8_state + s11_state + \
            s10_state
        # 用于s3
        err_inquiry_s3_exit = \
            DHT11_state_1 + DHT11_state_2 + \
            DHT11_state_3 + DHT11_state_4 + err_inquiry_function(11) + \
            err_inquiry_function(12) + s2_state_13 + err_inquiry_function(14) + \
            err_inquiry_function(15) + err_inquiry_function(16) + \
            DHT11_state_21 + \
            s13_state + \
            s12_state + s11_state + \
            s10_state
        # 用于s3,和手动状态下的首次复位
        err_inquiry_s4_s10 = \
            DHT11_state_1 + DHT11_state_2 + \
            DHT11_state_3 + DHT11_state_4 + err_inquiry_function(11) + \
            err_inquiry_function(12) + s2_state_13 + err_inquiry_function(14) + \
            err_inquiry_function(15) + err_inquiry_function(16) + \
            DHT11_state_21 + s2_state + \
            s1_state + s13_state + s12_state + \
            s11_state + s10_state
        # s4_s10
        err_inquiry_s10_s13 = \
            DHT11_state_1 + DHT11_state_2 + \
            DHT11_state_3 + DHT11_state_4 + err_inquiry_function(11) + \
            err_inquiry_function(12) + s2_state_13 + err_inquiry_function(14) + \
            err_inquiry_function(15) + err_inquiry_function(16) + s5_state + \
            DHT11_state_21 + s4_state + s2_state + \
            s1_state + s9_state + \
            s8_state + \
            s7_state + s6_state
        # s10_s13
        err_inquiry_s14 = \
            DHT11_state_1 + DHT11_state_2 + \
            DHT11_state_3 + DHT11_state_4 + \
            err_inquiry_function(12) + s2_state_13 + err_inquiry_function(14) + \
            err_inquiry_function(15) + err_inquiry_function(16) + \
            DHT11_state_21 + \
            s13_state + \
            s12_state + s11_state + \
            s10_state
        # 用于自动状态下的首次复位,还有自动清洗
        err_inquiry_s14_13 = \
            DHT11_state_1 + DHT11_state_2 + \
            DHT11_state_3 + DHT11_state_4 + err_inquiry_function(11) + \
            err_inquiry_function(12) + err_inquiry_function(14) + \
            err_inquiry_function(15) + err_inquiry_function(16) + s5_state + \
            DHT11_state_21 + s4_state + s2_state + \
            s1_state + s13_state + s9_state + \
            s12_state + s8_state + s11_state + \
            s7_state + s10_state + s6_state
        # s14的do13专用
        err_inquiry_s15 = \
            DHT11_state_1 + DHT11_state_2 + \
            DHT11_state_3 + DHT11_state_4 + err_inquiry_function(11) + \
            err_inquiry_function(12) + s2_state_13 + err_inquiry_function(14) + \
            err_inquiry_function(15) + err_inquiry_function(16) + \
            DHT11_state_21 + \
            s13_state + \
            s12_state + s11_state + \
            s10_state
        # s15专用
        err_inquiry_14 = \
            DHT11_state_1 + DHT11_state_2 + \
            DHT11_state_3 + DHT11_state_4 + err_inquiry_function(11) + \
            err_inquiry_function(12) + s2_state_13 + \
            err_inquiry_function(15) + err_inquiry_function(16) + s5_state + \
            DHT11_state_21 + s4_state + s2_state + \
            s1_state + s13_state + s9_state + \
            s12_state + s8_state + s11_state + \
            s7_state + s10_state + s6_state
        # 清洗液低液位报警专用
        err_inquiry_15 = \
            DHT11_state_1 + DHT11_state_2 + \
            DHT11_state_3 + DHT11_state_4 + err_inquiry_function(11) + \
            err_inquiry_function(12) + s2_state_13 + err_inquiry_function(14) + \
            err_inquiry_function(16) + s5_state + \
            DHT11_state_21 + s4_state + s2_state + \
            s1_state + s13_state + s9_state + \
            s12_state + s8_state + s11_state + \
            s7_state + s10_state + s6_state
        # 保护液低液位报警专用
        err_inquiry_16 = \
            DHT11_state_1 + DHT11_state_2 + \
            DHT11_state_3 + DHT11_state_4 + err_inquiry_function(11) + \
            err_inquiry_function(12) + s2_state_13 + err_inquiry_function(14) + \
            err_inquiry_function(15) + s5_state + \
            DHT11_state_21 + s4_state + s2_state + \
            s1_state + s13_state + s9_state + \
            s12_state + s8_state + s11_state + \
            s7_state + s10_state + s6_state
        # 缓冲液低液位报警专用
        err_inquiry_21 = \
            DHT11_state_1 + DHT11_state_2 + \
            DHT11_state_3 + DHT11_state_4 + err_inquiry_function(11) + \
            err_inquiry_function(12) + s2_state_13 + err_inquiry_function(14) + \
            err_inquiry_function(15) + err_inquiry_function(16) + s5_state + \
            s4_state + s2_state + \
            s1_state + s13_state + s9_state + \
            s12_state + s8_state + s11_state + \
            s7_state + s10_state + s6_state
        # 温湿度报警专用
        err_inquiry_1_4 = \
            err_inquiry_function(11) + \
            err_inquiry_function(12) + s2_state_13 + err_inquiry_function(14) + \
            err_inquiry_function(15) + err_inquiry_function(16) + s5_state + \
            DHT11_state_21 + s4_state + s2_state + \
            s1_state + s13_state + s9_state + \
            s12_state + s8_state + s11_state + \
            s7_state + s10_state + s6_state
        # 温湿度报警专用


def manual_control():
    manual_control_io_restart_t = 0  # 用于手动状态下查询io状态的复位
    manual_control_reset = 0  # 刚切到手动状态，复位一次
    global serial_txt, recharge_t, err_inquiry_s3_exit
    while 1:
        global do_location, di_location
        global manual_status, mode
        if mode == 1:
            manual_status = 0
            break
        if manual_control_reset == 0:
            manual_control_reset = 1
            dox_command([0], err_inquiry_s3_exit)  # 复位do
            # injection_port_0()  # 切换阀至0位

        if serial_txt == "050101":
            print("测量")
            serial_txt = 0
            manual_measurement_s1()  # 测量

        if serial_txt == "050201":
            print("检修")
            serial_txt = 0
            manual_maintenance_s2()  # 检修

        if serial_txt == "050301":
            manual_curing_s3()  # 手动保养
            dox_command([0], err_inquiry_s3_exit)  # 复位do
            # injection_port_0()  # 切换阀至0位
            serial_txt = 0

        if serial_txt == "070101":
            separate_water_flushing_s4()  # 单独水冲洗
            serial_txt = 0

        if serial_txt == "070201":
            online_water_flushing_s5()  # 在线水冲洗
            serial_txt = 0

        if serial_txt == "060101":
            clean_pump_s6()  # 输送清洗液
            serial_txt = 0

        if serial_txt == "060301":
            protect_pump_s7()  # 输送保护液
            serial_txt = 0

        if serial_txt == "060201":
            liquid_pump_4_s8()  # 输送缓冲液4
            serial_txt = 0

        if serial_txt == "060401":
            liquid_pump_6_s9()  # 输送缓冲液6
            serial_txt = 0

        time.sleep(0.1)
        manual_control_io_restart_t = manual_control_io_restart_t + 1
        if manual_control_io_restart_t >= 300 and recharge_t == 0:
            manual_control_io_restart_t = 0
            print(i2c40, i2c41, i2c42, i2c43)
            io_restart()


def manual_measurement_s1():
    global err_inquiry_s1, serial_txt, mode, s1_state, s2_state_13
    io_restart()
    s1_t = 0  # 计时使用
    s1_state = [24]
    s2_state_13 = [0]
    dox_command([24], err_inquiry_s1)  # 复位并输出DO24
    # # serial_val_modify("page0.now_position.txt=\"手动测量\"")
    serial_val_modify("homepage.now_status.txt=\"测量开启中\"")
    serial_val_modify("homepage.now_status.txt=\"测量开启中\"")
    time.sleep(0.1)
    while s1_t <= 60:  # 时限一分钟
        if serial_txt == "050100" or serial_txt == "050201":
            s1_state = [0]
            dox_command([0], err_inquiry_s1)  # 复位DO24
            serial_val_modify("homepage.now_status.txt=\"测量退出\"")
            serial_val_modify("homepage.now_status.txt=\"测量退出\"")
            return
        if di_location[4:5][0] == "1" or mode == 1:
            s1_state = [0]
            dox_command([0], err_inquiry_s1)  # 复位DO24
            # # serial_val_modify("page5.measure.val=0")
            serial_val_modify("homepage.now_status.txt=\"测量位\"")
            serial_val_modify("homepage.now_status.txt=\"测量位\"")
            s1_t = 70  # 成功后跳出循环
            return
        else:
            s1_t = s1_t + 1  # 没成功时间计数+1
            time.sleep(1)
    if s1_t > 60 and di_location[4:5][0] == "0":
        time1 = time.strftime('%H:%M:%S', time.localtime(time.time()))
        serial_val_modify("warningpage.data0.insert(\"%s^测量故障\")" % time1)  # 存入故障信息
        serial_val_modify("homepage.now_status.txt=\"测量故障\"")
        # # serial_val_modify("page0.now_position.txt=\"测量故障\"")
        s1_state = [0]
        dox_command([12], err_inquiry_s1)  # 输出do12
        # # serial_val_modify("page5.measure.val=0")
        # serial_val_modify("homepage.now_status.txt=\"输送故障\"")
        return


def manual_maintenance_s2():
    io_restart()
    global err_inquiry_s2, serial_txt, mode, s2_state, s2_state_13
    s2_t = 0  # 计时使用
    s2_state = [23]
    dox_command([23], err_inquiry_s2)  # 复位并输出DO19
    # # serial_val_modify("page0.now_position.txt=\"手动检修\"")
    serial_val_modify("homepage.now_status.txt=\"检修开启中\"")
    serial_val_modify("homepage.now_status.txt=\"检修开启中\"")
    time.sleep(0.1)
    while s2_t <= 60:  # 时限一分钟
        if serial_txt == "050200" or serial_txt == "050101":
            s2_state = [0]
            dox_command([0], err_inquiry_s2)  # 复位DO19
            serial_val_modify("homepage.now_status.txt=\"检修退出\"")
            serial_val_modify("homepage.now_status.txt=\"检修退出\"")
            return
        if di_location[5:6][0] == "1" or mode == 1:
            s2_state = [0]
            s2_state_13 = [13]
            dox_command([13], err_inquiry_s2)  # 输出do2
            s2_t = 70  # 成功后跳出循环
            # serial_val_modify("page5.overhaul.val=0")
            serial_val_modify("homepage.now_status.txt=\"检修位\"")
            serial_val_modify("homepage.now_status.txt=\"检修位\"")
            return
        else:
            s2_t = s2_t + 1  # 没成功时间计数+1
            time.sleep(1)
    if s2_t > 60 and di_location[5:6][0] == "0":
        time1 = time.strftime('%H:%M:%S', time.localtime(time.time()))
        serial_val_modify("warningpage.data0.insert(\"%s^检修故障\")" % time1)  # 存入故障信息
        serial_val_modify("homepage.now_status.txt=\"检修故障\"")
        # # serial_val_modify("page0.now_position.txt=\"检修故障\"")
        s2_state = [0]
        dox_command([12], err_inquiry_s2)  # 输出do3
        # serial_val_modify("page5.overhaul.val=0")
        # serial_val_modify("homepage.now_status.txt=\"输送故障\"")
        return


def manual_curing_s3():
    manual_curing_n = 1  # 用于记录保养的所处步骤
    serial_val_modify("homepage.now_status.txt=\"保养中\"")
    serial_val_modify("homepage.now_status.txt=\"保养中\"")
    while 1:
        global err_inquiry_s3, serial_txt, err_inquiry_s3_exit
        global mode, atcal, flu, Tcx, Tqx1, Tqx2, Tbh, Tby, Thc, Txh
        global di_location

        if mode == 1 or serial_txt == "050300":
            dox_command([0], err_inquiry_s3_exit)  # 复位do
            # injection_port_0()  # 切换阀至0位
            serial_val_modify("homepage.now_status.txt=\"保养结束\"")
            serial_val_modify("homepage.now_status.txt=\"保养结束\"")
            #  # serial_val_modify("page0.now_status.txt=\"就地保养结束\"")
            return

        if manual_curing_n == 1:
            print("步骤1")
            dox_command([13], err_inquiry_s3)
            #  # serial_val_modify("page0.now_status.txt=\"就地保养中\"")
            print("步骤2")
            if flu == 1:
                manual_curing_n = 3
            elif flu == 0:
                manual_curing_n = 4

        if mode == 1 or serial_txt == "050300":
            dox_command([0], err_inquiry_s3_exit)  # 复位do
            # injection_port_0()  # 切换阀至0位
            serial_val_modify("homepage.now_status.txt=\"保养结束\"")
            serial_val_modify("homepage.now_status.txt=\"保养结束\"")
            #  # serial_val_modify("page0.now_status.txt=\"就地保养结束\"")
            return

        if manual_curing_n == 3:
            manual_curing_t3 = 0
            print("步骤3")
            dox_command([13, 20], err_inquiry_s3)
            while manual_curing_t3 <= 29:
                print("步骤3时停")
                manual_curing_t3 = manual_curing_t3 + 1
                time.sleep(1)
                if mode == 1 or serial_txt == "050300":
                    dox_command([0], err_inquiry_s3_exit)  # 复位do
                    # injection_port_0()  # 切换阀至0位
                    serial_val_modify("homepage.now_status.txt=\"保养结束\"")
                    serial_val_modify("homepage.now_status.txt=\"保养结束\"")
                    #  # serial_val_modify("page0.now_status.txt=\"就地保养结束\"")
                    return
            manual_curing_n = 4

        if manual_curing_n == 4:
            print("步骤4")
            injection_port(4)
            if insert_database() == "ac03":  # 查询指令
                manual_curing_n = 5
                # # serial_val_modify("page0.now_position.txt=\"切换阀水冲洗位置\"")
                pass  # 成功
            else:
                if insert_database() == "ac03":  # 查询指令
                    manual_curing_n = 5
                    # # serial_val_modify("page0.now_position.txt=\"切换阀水冲洗位置\"")
                    pass  # 成功
                else:
                    time1 = time.strftime('%H:%M:%S', time.localtime(time.time()))
                    serial_val_modify("warningpage.data0.insert(\"%s^切换阀水冲洗位故障\")" % time1)  # 存入故障信息
                    # # serial_val_modify("page0.now_position.txt=\"切换阀水冲洗位故障\"")
                    # serial_val_modify("homepage.now_status.txt=\"输送故障\"")
                    return

        io_restart()  # 复位下dodi的读取
        time.sleep(1)
        if mode == 1 or serial_txt == "050300":
            dox_command([0], err_inquiry_s3_exit)  # 复位do
            # injection_port_0()  # 切换阀至0位
            serial_val_modify("homepage.now_status.txt=\"保养结束\"")
            serial_val_modify("homepage.now_status.txt=\"保养结束\"")
            #  # serial_val_modify("page0.now_status.txt=\"就地保养结束\"")
            return

        if manual_curing_n == 5:
            print("步骤5")
            dox_command([13, 20, 22], err_inquiry_s3)  # 输出do25
            time.sleep(2)
            manual_curing_n = 6

        if mode == 1 or serial_txt == "050300":
            dox_command([0], err_inquiry_s3_exit)  # 复位do
            # injection_port_0()  # 切换阀至0位
            serial_val_modify("homepage.now_status.txt=\"保养结束\"")
            serial_val_modify("homepage.now_status.txt=\"保养结束\"")
            #  # serial_val_modify("page0.now_status.txt=\"就地保养结束\"")
            return

        if manual_curing_n == 6:
            print("步骤6")
            dox_command([13, 20, 22, 23], err_inquiry_s3)  # 输出do19
            manual_curing_n = 7

        if manual_curing_n == 7:
            print("步骤7")
            s2_t = 0  # 计时使用
            time.sleep(0.1)
            while s2_t <= 60:  # 时限一分钟
                if mode == 1 or serial_txt == "050300":
                    dox_command([0], err_inquiry_s3_exit)  # 复位do
                    # injection_port_0()  # 切换阀至0位
                    serial_val_modify("homepage.now_status.txt=\"保养结束\"")
                    serial_val_modify("homepage.now_status.txt=\"保养结束\"")
                    #  # serial_val_modify("page0.now_status.txt=\"就地保养结束\"")
                    return
                if di_location[5:6][0] == "1":
                    dox_command([13, 22], err_inquiry_s3)  # 复位do19，26
                    serial_val_modify("homepage.state_sw.val=1")
                    s2_t = 70  # 成功后跳出循环
                    break
                else:
                    s2_t = s2_t + 1  # 没成功时间计数+1
                    time.sleep(1)
            if s2_t > 60 and di_location[5:6][0] == "0":
                time1 = time.strftime('%H:%M:%S', time.localtime(time.time()))
                serial_val_modify("warningpage.data0.insert(\"%s^检修故障\")" % time1)  # 存入故障信息
                # # serial_val_modify("page0.now_position.txt=\"检修故障\"")
                dox_command([12], err_inquiry_s3)  # 输出do3
                # serial_val_modify("homepage.now_status.txt=\"输送故障\"")
                return
            manual_curing_n = 8

        if mode == 1 or serial_txt == "050300":
            dox_command([0], err_inquiry_s3_exit)  # 复位do
            # injection_port_0()  # 切换阀至0位
            serial_val_modify("homepage.now_status.txt=\"保养结束\"")
            serial_val_modify("homepage.now_status.txt=\"保养结束\"")
            #  # serial_val_modify("page0.now_status.txt=\"就地保养结束\"")
            return

        io_restart()  # 复位下dido的问询
        time.sleep(1)
        if manual_curing_n == 8:
            print("步骤8")
            manual_curing_t8 = 0
            while manual_curing_t8 <= Tcx:
                manual_curing_t8 = manual_curing_t8 + 1
                time.sleep(1)
                print("步骤8时停")
                if mode == 1 or serial_txt == "050300":
                    dox_command([0], err_inquiry_s3_exit)  # 复位do
                    # injection_port_0()  # 切换阀至0位
                    serial_val_modify("homepage.now_status.txt=\"保养结束\"")
                    serial_val_modify("homepage.now_status.txt=\"保养结束\"")
                    #  # serial_val_modify("page0.now_status.txt=\"就地保养结束\"")
                    return
            manual_curing_n = 9

        if mode == 1 or serial_txt == "050300":
            dox_command([0], err_inquiry_s3_exit)  # 复位do
            # injection_port_0()  # 切换阀至0位
            serial_val_modify("homepage.now_status.txt=\"保养结束\"")
            serial_val_modify("homepage.now_status.txt=\"保养结束\"")
            #  # serial_val_modify("page0.now_status.txt=\"就地保养结束\"")
            return

        if manual_curing_n == 9:
            print("步骤9")
            dox_command([13], err_inquiry_s3)  # 复位do25
            injection_port(1)

            if insert_database() == "ab02":  # 查询指令
                manual_curing_n = 10
                # # serial_val_modify("page0.now_position.txt=\"切换阀x-1位置\"")
                pass  # 成功
            else:
                if insert_database() == "ab02":  # 查询指令
                    manual_curing_n = 10
                    # # serial_val_modify("page0.now_position.txt=\"切换阀x-1位置\"")
                    pass  # 成功
                else:
                    time1 = time.strftime('%H:%M:%S', time.localtime(time.time()))
                    serial_val_modify("warningpage.data0.insert(\"%s^切换阀x-1位故障\")" % time1)  # 存入故障信息
                    # # serial_val_modify("page0.now_position.txt=\"切换阀x-1位故障\"")
                    # serial_val_modify("homepage.now_status.txt=\"输送故障\"")
                    return

        if mode == 1 or serial_txt == "050300":
            dox_command([0], err_inquiry_s3_exit)  # 复位do
            # injection_port_0()  # 切换阀至0位
            serial_val_modify("homepage.now_status.txt=\"保养结束\"")
            serial_val_modify("homepage.now_status.txt=\"保养结束\"")
            #  # serial_val_modify("page0.now_status.txt=\"就地保养结束\"")
            return

        if manual_curing_n == 10:
            print("步骤10")
            dox_command([13, 32], err_inquiry_s3)  # 输出do20
            manual_curing_t10 = 0
            while manual_curing_t10 <= Tqx1:
                manual_curing_t10 = manual_curing_t10 + 1
                time.sleep(1)
                print("步骤10时停")
                if mode == 1 or serial_txt == "050300":
                    dox_command([0], err_inquiry_s3_exit)  # 复位do
                    # injection_port_0()  # 切换阀至0位
                    serial_val_modify("homepage.now_status.txt=\"保养结束\"")
                    serial_val_modify("homepage.now_status.txt=\"保养结束\"")
                    #  # serial_val_modify("page0.now_status.txt=\"就地保养结束\"")
                    return
            manual_curing_n = 11

        io_restart()  # 复位下dido的问询
        time.sleep(1)
        if manual_curing_n == 11:
            print("步骤11")
            dox_command([13], err_inquiry_s3)  # 复位do20
            manual_curing_t11 = 0
            s3_io_res_11 = 0
            while manual_curing_t11 <= Tqx2:
                manual_curing_t11 = manual_curing_t11 + 1
                time.sleep(1)
                print("步骤11时停")
                s3_io_res_11 = s3_io_res_11 + 1
                if s3_io_res_11 >= 60:
                    s3_io_res_11 = 0
                    io_restart()  # 复位下dido的问询
                    time.sleep(1)
                if mode == 1 or serial_txt == "050300":
                    dox_command([0], err_inquiry_s3_exit)  # 复位do
                    # injection_port_0()  # 切换阀至0位
                    serial_val_modify("homepage.now_status.txt=\"保养结束\"")
                    serial_val_modify("homepage.now_status.txt=\"保养结束\"")
                    #  # serial_val_modify("page0.now_status.txt=\"就地保养结束\"")
                    return
            manual_curing_n = 12

        if manual_curing_n == 12:
            dox_command([13, 30], err_inquiry_s3)  # 输出do21
            manual_curing_t12 = 0
            while manual_curing_t12 <= Tbh:
                manual_curing_t12 = manual_curing_t12 + 1
                time.sleep(1)
                print("步骤12时停")
                if mode == 1 or serial_txt == "050300":
                    dox_command([0], err_inquiry_s3_exit)  # 复位do
                    # injection_port_0()  # 切换阀至0位
                    serial_val_modify("homepage.now_status.txt=\"保养结束\"")
                    serial_val_modify("homepage.now_status.txt=\"保养结束\"")
                    #  # serial_val_modify("page0.now_status.txt=\"就地保养结束\"")
                    return
            manual_curing_n = 13

        if manual_curing_n == 13:
            dox_command([13], err_inquiry_s3)  # 复位do21
            # injection_port_0()
            manual_curing_n = 14

        if manual_curing_n == 14:
            T1 = 0
            s3_io_res_14 = 0
            while T1 <= Tby:
                T1 = T1 + 1
                time.sleep(1)
                s3_io_res_14 = s3_io_res_14 + 1
                if s3_io_res_14 >= 60:
                    s3_io_res_14 = 0
                    io_restart()  # 复位下dido的问询
                    time.sleep(1)
                print("步骤14时停")
                if mode == 1 or serial_txt == "050300":
                    dox_command([0], err_inquiry_s3_exit)  # 复位do
                    # injection_port_0()  # 切换阀至0位
                    serial_val_modify("homepage.now_status.txt=\"保养结束\"")
                    serial_val_modify("homepage.now_status.txt=\"保养结束\"")
                    #  # serial_val_modify("page0.now_status.txt=\"就地保养结束\"")
                    return
            manual_curing_n = 12


def separate_water_flushing_s4():
    global err_inquiry_s4_s10, serial_txt, mode, s4_state
    # # serial_val_modify("page0.now_position.txt=\"单独水冲洗中\"")
    serial_val_modify("localpage.cavity_txt.txt=\"准备中\"")
    serial_val_modify("localpage.cavity_txt.txt=\"准备中\"")
    injection_port(4)
    s4_t1 = 1
    while serial_txt == "070100" or mode == 1:
        serial_val_modify("localpage.cavity_txt.txt=\"输送结束\"")
        serial_val_modify("localpage.cavity_txt.txt=\"输送结束\"")
        return
    while s4_t1 <= 0:
        print("时停5秒")
        s4_t1 = s4_t1 + 1
        time.sleep(0.1)
        if serial_txt == "070100" or mode == 1:
            serial_val_modify("localpage.cavity_txt.txt=\"输送结束\"")
            break
    if insert_database() == "ac03":  # 查询指令
        # # serial_val_modify("page0.now_position.txt=\"切换阀水冲洗位置\"")
        pass  # 成功
    else:
        if insert_database() == "ac03":  # 查询指令
            # # serial_val_modify("page0.now_position.txt=\"切换阀水冲洗位置\"")
            pass  # 成功
        else:
            time1 = time.strftime('%H:%M:%S', time.localtime(time.time()))
            serial_val_modify("warningpage.data0.insert(\"%s^切换阀水冲洗位故障\")" % time1)  # 存入故障信息
            # # serial_val_modify("page0.now_position.txt=\"切换阀水冲洗位故障\"")
            serial_val_modify("localpage.cavity_txt.txt=\"输送故障\"")
            serial_val_modify("localpage.cavity_txt.txt=\"输送故障\"")
            return
    while serial_txt == "070100" or mode == 1:
        serial_val_modify("localpage.cavity_txt.txt=\"输送结束\"")
        serial_val_modify("localpage.cavity_txt.txt=\"输送结束\"")
        return
    serial_val_modify("localpage.cavity_txt.txt=\"输送中\"")
    serial_val_modify("localpage.cavity_txt.txt=\"输送中\"")
    s4_state = [22]
    dox_command([22], err_inquiry_s4_s10)  # 输出do222
    while serial_txt != "070100":
        time.sleep(0.1)
        if mode == 1:
            break
    s4_state = [0]
    dox_command([0], err_inquiry_s4_s10)  # 复位do25
    # injection_port_0()
    # # serial_val_modify("page0.now_position.txt=\"单独水冲洗结束\"")
    serial_val_modify("localpage.cavity_txt.txt=\"输送结束\"")
    serial_val_modify("localpage.cavity_txt.txt=\"输送结束\"")


def online_water_flushing_s5():
    global err_inquiry_s4_s10, serial_txt, mode, s5_state
    # # serial_val_modify("page0.now_position.txt=\"在线水冲洗中\"")
    serial_val_modify("localpage.online_txt.txt=\"输送中\"")
    serial_val_modify("localpage.online_txt.txt=\"输送中\"")
    s5_state = [20]
    dox_command([20], err_inquiry_s4_s10)  # 输出do26
    while serial_txt != "070200":
        time.sleep(0.1)
        if mode == 1:
            break
    s5_state = [0]
    dox_command([0], err_inquiry_s4_s10)  # 复位do26
    # # serial_val_modify("page0.now_position.txt=\"在线水冲洗结束\"")
    serial_val_modify("localpage.online_txt.txt=\"输送结束\"")
    serial_val_modify("localpage.online_txt.txt=\"输送结束\"")


def clean_pump_s6():
    global err_inquiry_s4_s10, serial_txt, mode, s6_state
    # # serial_val_modify("page0.now_position.txt=\"输送清洗液中\"")
    serial_val_modify("localpage.clean_pump_txt.txt=\"准备中\"")
    serial_val_modify("localpage.clean_pump_txt.txt=\"准备中\"")
    injection_port(1)
    s6_t1 = 1
    while serial_txt == "060100" or mode == 1:
        serial_val_modify("localpage.clean_pump_txt.txt=\"输送结束\"")
        serial_val_modify("localpage.clean_pump_txt.txt=\"输送结束\"")
        return
    while s6_t1 <= 0:
        print("时停5秒")
        s6_t1 = s6_t1 + 1
        time.sleep(0.1)
        if serial_txt == "060100" or mode == 1:
            break
    if insert_database() == "ab02":  # 查询指令
        # # serial_val_modify("page0.now_position.txt=\"切换阀x-1位置\"")
        pass  # 成功
    else:
        if insert_database() == "ab02":  # 查询指令
            # # serial_val_modify("page0.now_position.txt=\"切换阀x-1位置\"")
            pass  # 成功
        else:
            time1 = time.strftime('%H:%M:%S', time.localtime(time.time()))
            serial_val_modify("warningpage.data0.insert(\"%s^切换阀x-1位故障\")" % time1)  # 存入故障信息
            # # serial_val_modify("page0.now_position.txt=\"切换阀x-1位故障\"")
            serial_val_modify("localpage.clean_pump_txt.txt=\"输送故障\"")
            serial_val_modify("localpage.clean_pump_txt.txt=\"输送故障\"")
            return
    while serial_txt == "060100" or mode == 1:
        serial_val_modify("localpage.clean_pump_txt.txt=\"输送结束\"")
        serial_val_modify("localpage.clean_pump_txt.txt=\"输送结束\"")
        return
    serial_val_modify("localpage.clean_pump_txt.txt=\"输送中\"")
    serial_val_modify("localpage.clean_pump_txt.txt=\"输送中\"")
    s6_state = [32]
    dox_command([32], err_inquiry_s4_s10)  # 输出do20
    while serial_txt != "060100":
        time.sleep(0.1)
        if mode == 1:
            break
    s6_state = [0]
    dox_command([0], err_inquiry_s4_s10)  # 复位do20
    # injection_port_0()
    # # serial_val_modify("page0.now_position.txt=\"输送清洗液结束\"")
    serial_val_modify("localpage.clean_pump_txt.txt=\"输送结束\"")
    serial_val_modify("localpage.clean_pump_txt.txt=\"输送结束\"")


def protect_pump_s7():
    global err_inquiry_s4_s10, serial_txt, mode, s7_state
    # # serial_val_modify("page0.now_position.txt=\"输送保护液中\"")
    serial_val_modify("localpage.pro_pump_txt.txt=\"准备中\"")
    serial_val_modify("localpage.pro_pump_txt.txt=\"准备中\"")
    injection_port(1)
    s7_t1 = 1
    while serial_txt == "060300" or mode == 1:
        serial_val_modify("localpage.pro_pump_txt.txt=\"输送结束\"")
        serial_val_modify("localpage.pro_pump_txt.txt=\"输送结束\"")
        return
    while s7_t1 <= 0:
        print("时停5秒")
        s7_t1 = s7_t1 + 1
        time.sleep(0.1)
        if serial_txt == "060300" or mode == 1:
            break
    if insert_database() == "ab02":  # 查询指令
        # # serial_val_modify("page0.now_position.txt=\"切换阀x-1位置\"")
        pass  # 成功
    else:
        if insert_database() == "ab02":  # 查询指令
            # # serial_val_modify("page0.now_position.txt=\"切换阀x-1位置\"")
            pass  # 成功
        else:
            time1 = time.strftime('%H:%M:%S', time.localtime(time.time()))
            serial_val_modify("warningpage.data0.insert(\"%s^切换阀x-1位故障\")" % time1)  # 存入故障信息
            # # serial_val_modify("page0.now_position.txt=\"切换阀x-1位故障\"")
            serial_val_modify("localpage.pro_pump_txt.txt=\"输送故障\"")
            serial_val_modify("localpage.pro_pump_txt.txt=\"输送故障\"")
            return
    while serial_txt == "060300" or mode == 1:
        serial_val_modify("localpage.pro_pump_txt.txt=\"输送结束\"")
        serial_val_modify("localpage.pro_pump_txt.txt=\"输送结束\"")
        return
    serial_val_modify("localpage.pro_pump_txt.txt=\"输送中\"")
    serial_val_modify("localpage.pro_pump_txt.txt=\"输送中\"")
    s7_state = [30]
    dox_command([30], err_inquiry_s4_s10)  # 输出do21
    while serial_txt != "060300":
        time.sleep(0.1)
        if mode == 1:
            break
    s7_state = [0]
    dox_command([0], err_inquiry_s4_s10)  # 复位do21
    # injection_port_0()
    # # serial_val_modify("page0.now_position.txt=\"输送保护液结束\"")
    serial_val_modify("localpage.pro_pump_txt.txt=\"输送结束\"")
    serial_val_modify("localpage.pro_pump_txt.txt=\"输送结束\"")


def liquid_pump_4_s8():
    global err_inquiry_s4_s10, serial_txt, mode, s8_state
    # # serial_val_modify("page0.now_position.txt=\"输送缓冲液4中\"")
    serial_val_modify("localpage.pump_4_txt.txt=\"准备中\"")
    serial_val_modify("localpage.pump_4_txt.txt=\"准备中\"")
    injection_port(3)
    s8_t1 = 1
    while serial_txt == "060200" or mode == 1:
        serial_val_modify("localpage.pump_4_txt.txt=\"输送结束\"")
        serial_val_modify("localpage.pump_4_txt.txt=\"输送结束\"")
        return
    while s8_t1 <= 0:
        print("时停5秒")
        s8_t1 = s8_t1 + 1
        time.sleep(0.1)
        if serial_txt == "060200" or mode == 1:
            break
    if insert_database() == "af06":  # 查询指令
        # # serial_val_modify("page0.now_position.txt=\"切换阀x-3位置\"")
        pass  # 成功
    else:
        if insert_database() == "af06":  # 查询指令
            # # serial_val_modify("page0.now_position.txt=\"切换阀x-3位置\"")
            pass  # 成功
        else:
            time1 = time.strftime('%H:%M:%S', time.localtime(time.time()))
            serial_val_modify("warningpage.data0.insert(\"%s^切换阀x-3位故障\")" % time1)  # 存入故障信息
            # # serial_val_modify("page0.now_position.txt=\"切换阀x-3位故障\"")
            serial_val_modify("localpage.pump_4_txt.txt=\"输送故障\"")
            serial_val_modify("localpage.pump_4_txt.txt=\"输送故障\"")
            return
    while serial_txt == "060200" or mode == 1:
        serial_val_modify("localpage.pump_4_txt.txt=\"输送结束\"")
        serial_val_modify("localpage.pump_4_txt.txt=\"输送结束\"")
        return
    serial_val_modify("localpage.pump_4_txt.txt=\"输送中\"")
    serial_val_modify("localpage.pump_4_txt.txt=\"输送中\"")
    s8_state = [28]
    dox_command([28], err_inquiry_s4_s10)  # 输出do23
    while serial_txt != "060200":
        time.sleep(0.1)
        if mode == 1:
            break
    s8_state = [0]
    dox_command([0], err_inquiry_s4_s10)  # 复位do23
    # injection_port_0()
    # # serial_val_modify("page0.now_position.txt=\"输送缓冲液4结束\"")
    serial_val_modify("localpage.pump_4_txt.txt=\"输送结束\"")
    serial_val_modify("localpage.pump_4_txt.txt=\"输送结束\"")


def liquid_pump_6_s9():
    global err_inquiry_s4_s10, serial_txt, mode, s9_state
    # # serial_val_modify("page0.now_position.txt=\"输送缓冲液6中\"")
    serial_val_modify("localpage.pump_6_txt.txt=\"准备中\"")
    serial_val_modify("localpage.pump_6_txt.txt=\"准备中\"")
    injection_port(2)
    while serial_txt == "060400" or mode == 1:
        serial_val_modify("localpage.pump_6_txt.txt=\"输送结束\"")
        serial_val_modify("localpage.pump_6_txt.txt=\"输送结束\"")
        return
    s9_t1 = 1
    while s9_t1 <= 0:
        print("时停5秒")
        s9_t1 = s9_t1 + 1
        time.sleep(0.1)
        if serial_txt == "060400" or mode == 1:
            break
    if insert_database() == "aa01":  # 查询指令
        # # serial_val_modify("page0.now_position.txt=\"切换阀x-2位置\"")
        pass  # 成功
    else:
        if insert_database() == "aa01":  # 查询指令
            # # serial_val_modify("page0.now_position.txt=\"切换阀x-2位置\"")
            pass  # 成功
        else:
            time1 = time.strftime('%H:%M:%S', time.localtime(time.time()))
            serial_val_modify("warningpage.data0.insert(\"%s^切换阀x-2位故障\")" % time1)  # 存入故障信息
            # # serial_val_modify("page0.now_position.txt=\"切换阀x-2位故障\"")
            serial_val_modify("localpage.pump_6_txt.txt=\"输送故障\"")
            return
    while serial_txt == "060400" or mode == 1:
        serial_val_modify("localpage.pump_6_txt.txt=\"输送结束\"")
        serial_val_modify("localpage.pump_6_txt.txt=\"输送结束\"")
        return
    serial_val_modify("localpage.pump_6_txt.txt=\"输送中\"")
    serial_val_modify("localpage.pump_6_txt.txt=\"输送中\"")
    s9_state = [26]
    dox_command([26], err_inquiry_s4_s10)  # 输出do24
    while serial_txt != "060400":
        time.sleep(0.1)
        if mode == 1:
            break
    s9_state = [0]
    dox_command([0], err_inquiry_s4_s10)  # 复位do24
    # injection_port_0()
    # # serial_val_modify("page0.now_position.txt=\"输送缓冲液6结束\"")
    serial_val_modify("localpage.pump_6_txt.txt=\"输送结束\"")
    serial_val_modify("localpage.pump_6_txt.txt=\"输送结束\"")


def replenish_clean_supply_s10():
    global err_inquiry_s10_s13, serial_txt, mode, YW40H, i2c40, do_location, s10_state
    # # serial_val_modify("page0.now_position.txt=\"补充清洗液中\"")
    serial_val_modify("drugpage.clean_add_txt.txt=\"补充中\"")
    serial_val_modify("drugpage.clean_add_txt.txt=\"补充中\"")
    clean_supply_t = 0
    clean_supply_t2 = 0
    while i2c40 <= YW40H:
        if clean_supply_t == 0:
            clean_supply_t = 1
            s10_state = [31]
            dox_command([31], err_inquiry_s10_s13)  # 输出do27
        time.sleep(1)
        # water_content = (i2c40 / YW40H) * 100
        # serial_val_modify("drugpage.water_content.val=%s" % water_content)
        if i2c40 >= YW40H or serial_txt == "080100" or (GPIO.input(5) == 0 and GPIO.input(6) == 0):
            clean_supply_t2 = 1
            s10_state = [0]
            dox_command([0], err_inquiry_s10_s13)  # 复位do27
            serial_txt = 0
            serial_val_modify("drugpage.clean_add.val=0")
            break
    if clean_supply_t2 == 0 or do_location[30:31][0] == "1":
        s10_state = [0]
        dox_command([0], err_inquiry_s10_s13)  # 复位do27
        serial_txt = 0
        serial_val_modify("drugpage.clean_add.val=0")
    # # serial_val_modify("page0.now_position.txt=\"补充清洗液结束\"")
    serial_val_modify("drugpage.clean_add_txt.txt=\"补充结束\"")
    serial_val_modify("drugpage.clean_add_txt.txt=\"补充结束\"")


def replenish_protect_supply_s11():
    global err_inquiry_s10_s13, serial_txt, mode, YW41H, i2c41, do_location, s11_state
    # # serial_val_modify("page0.now_position.txt=\"补充保护液中\"")
    serial_val_modify("drugpage.pro_add_txt.txt=\"补充中\"")
    serial_val_modify("drugpage.pro_add_txt.txt=\"补充中\"")
    protect_supply_t = 0
    protect_supply_t2 = 0
    while i2c41 <= YW41H:
        if protect_supply_t == 0:
            protect_supply_t = 1
            s11_state = [29]
            dox_command([29], err_inquiry_s10_s13)  # 输出do28
        time.sleep(1)
        # water_content = (i2c41 / YW41H) * 100
        # serial_val_modify("drugpage.water_content.val=%s" % water_content)
        if i2c41 >= YW41H or serial_txt == "080300" or (GPIO.input(19) == 0 and GPIO.input(13) == 0):
            protect_supply_t2 = 1
            s11_state = [0]
            dox_command([0], err_inquiry_s10_s13)  # 复位do28
            serial_txt = 0
            serial_val_modify("drugpage.protect_add.val=0")
            break
    if protect_supply_t2 == 0 or do_location[28:29][0] == "1":
        s11_state = [0]
        dox_command([0], err_inquiry_s10_s13)  # 复位do28
        serial_txt = 0
        serial_val_modify("drugpage.protect_add.val=0")
    # # serial_val_modify("page0.now_position.txt=\"补充保护液结束\"")
    serial_val_modify("drugpage.pro_add_txt.txt=\"补充结束\"")
    serial_val_modify("drugpage.pro_add_txt.txt=\"补充结束\"")


def replenish_rehydration_4_s12():
    global err_inquiry_s10_s13, serial_txt, mode, YW42H, i2c42, do_location, s12_state
    # # serial_val_modify("page0.now_position.txt=\"补充缓冲液4中\"")
    serial_val_modify("drugpage.add_4_txt.txt=\"补充中\"")
    serial_val_modify("drugpage.add_4_txt.txt=\"补充中\"")
    rehydration_4_t = 0
    rehydration_4_t2 = 0
    while i2c42 <= YW42H:
        if rehydration_4_t == 0:
            rehydration_4_t = 1
            s12_state = [27]
            dox_command([27], err_inquiry_s10_s13)  # 输出do29
        time.sleep(1)
        # water_content = (i2c42 / YW42H) * 100
        # serial_val_modify("drugpage.water_content.val=%s" % water_content)
        if i2c42 >= YW42H or serial_txt == "080200" or (GPIO.input(20) == 0 and GPIO.input(16) == 0):
            rehydration_4_t2 = 1
            s12_state = [0]
            dox_command([0], err_inquiry_s10_s13)  # 复位do29
            serial_txt = 0
            serial_val_modify("drugpage.add_4.val=0")
            break
    if rehydration_4_t2 == 0 or do_location[26:27][0] == "1":
        s12_state = [0]
        dox_command([0], err_inquiry_s10_s13)  # 复位do29
        serial_txt = 0
        serial_val_modify("drugpage.add_4.val=0")
    # # serial_val_modify("page0.now_position.txt=\"补充缓冲液4结束\"")
    serial_val_modify("drugpage.add_4_txt.txt=\"补充结束\"")
    serial_val_modify("drugpage.add_4_txt.txt=\"补充结束\"")


def replenish_rehydration_6_s13():
    global err_inquiry_s10_s13, serial_txt, mode, YW43H, i2c43, do_location, err_inquiry_27_30, s13_state
    # # serial_val_modify("page0.now_position.txt=\"补充缓冲液6中\"")
    serial_val_modify("drugpage.add_6_txt.txt=\"补充中\"")
    serial_val_modify("drugpage.add_6_txt.txt=\"补充中\"")
    rehydration_6_t = 0
    rehydration_6_t2 = 0
    while i2c43 <= YW43H:
        if rehydration_6_t == 0:
            rehydration_6_t = 1
            s13_state = [25]
            dox_command([25], err_inquiry_s10_s13)  # 输出do30
        time.sleep(1)
        # water_content = (i2c43 / YW43H) * 100
        # serial_val_modify("drugpage.water_content.val=%s" % water_content)
        if i2c43 >= YW43H or serial_txt == "080400" or (GPIO.input(12) == 0 and GPIO.input(26) == 0):
            rehydration_6_t2 = 1
            s13_state = [0]
            dox_command([0], err_inquiry_s10_s13)  # 复位do30
            serial_txt = 0
            serial_val_modify("drugpage.add_6.val=0")
            break
    if rehydration_6_t2 == 0 or do_location[24:25][0] == "1":
        s13_state = [0]
        dox_command([0], err_inquiry_s10_s13)  # 复位do30
        serial_txt = 0
        serial_val_modify("drugpage.add_6.val=0")
    # # serial_val_modify("page0.now_position.txt=\"补充缓冲液6结束\"")
    serial_val_modify("drugpage.add_6_txt.txt=\"补充结束\"")
    serial_val_modify("drugpage.add_6_txt.txt=\"补充结束\"")


def automatic_cleaning_s14():
    global err_inquiry_s14_13, serial_txt, mode, di_location, automatic_cleaning_status
    global flu, atcal, err_inquiry_s14, do_location, recharge_t
    global Tqx1, Tqx2, Tby, Txh, Tco, Thc, err_inquiry_s14
    automatic_cleaning_n = 0
    C1 = 0
    C2 = 0
    C1_time = 0
    while 1:
        if automatic_cleaning_n == 0:
            s14_t0 = 0  # 计时使用
            while s14_t0 <= 50:  # 时限5秒
                if mode == 0 or di_location[0:1][0] == "1":
                    automatic_cleaning_status = 0
                    return
                s14_t0 = s14_t0 + 1  # 没成功时间计数+1
                time.sleep(0.1)
            print("自动清洗开启中")
            automatic_cleaning_n = 1

        if automatic_cleaning_n == 1:
            print("自动清洗步骤1")
            serial_val_modify("homepage.now_status.txt=\"自动清洗中\"")
            serial_val_modify("homepage.now_status.txt=\"自动清洗中\"")
            dox_command([0], err_inquiry_s14)
            # injection_port_0()  # 切换阀复位,X-0指令
            dox_command([11], err_inquiry_s14)  # 输出do11
            automatic_cleaning_n = 2

        if mode == 0 or di_location[0:1][0] == "1" or automatic_cleaning_n == 999:
            automatic_cleaning_status = 0
            return

        if automatic_cleaning_n == 2:
            print("自动清洗步骤2")
            injection_port(4)  # 切换阀复位,X-4指令
            time.sleep(5)
            if insert_database() == "ac03":  # 查询指令
                # # serial_val_modify("page0.now_position.txt=\"切换阀X-4位置\"")
                pass  # 成功
            else:
                time.sleep(3)
                if insert_database() == "ac03":  # 查询指令
                    # # serial_val_modify("page0.now_position.txt=\"切换阀X-4位置\"")
                    pass  # 成功
                else:
                    time1 = time.strftime('%H:%M:%S', time.localtime(time.time()))
                    serial_val_modify("warningpage.data0.insert(\"%s^切换阀故障\")" % time1)  # 存入故障信息
                    # # serial_val_modify("page0.now_position.txt=\"切换阀故障\"")
                    return
            automatic_cleaning_n = 3

        if mode == 0 or di_location[0:1][0] == "1":
            automatic_cleaning_status = 0
            return

        if automatic_cleaning_n == 3:
            print("自动清洗步骤3")
            dox_command([11, 22], err_inquiry_s14)  # 输出DO22
            time.sleep(2)
            automatic_cleaning_n = 4

        if mode == 0 or di_location[0:1][0] == "1":
            automatic_cleaning_status = 0
            return

        if automatic_cleaning_n == 4:
            print("自动清洗步骤4")
            dox_command([11, 22, 24], err_inquiry_s14)  # 输出DO24
            s14_t4 = 0  # 计时使用
            while s14_t4 <= 60:  # 时限一分钟
                if di_location[4:5][0] == "1" or mode == 0:
                    dox_command([11, 22], err_inquiry_s14)  # 复位DO24
                    s14_t4 = 70  # 成功后跳出循环
                    automatic_cleaning_n = 6
                    serial_val_modify("homepage.state_sw.val=0")
                    break
                else:
                    s14_t4 = s14_t4 + 1  # 没成功时间计数+1
                    time.sleep(1)
            if s14_t4 > 60 and di_location[4:5][0] == "0":
                time1 = time.strftime('%H:%M:%S', time.localtime(time.time()))
                serial_val_modify("warningpage.data0.insert(\"%s^测量故障\")" % time1)  # 存入故障信息
                # # serial_val_modify("page0.now_position.txt=\"测量故障\"")
                dox_command([3], err_inquiry_s14)  # 输出do3
                # # serial_val_modify("page5.measure.val=0")
                return

        if mode == 0 or di_location[0:1][0] == "1":
            automatic_cleaning_status = 0
            return

        if automatic_cleaning_n == 6:
            print("自动清洗步骤6")
            dox_command([11], err_inquiry_s14)  # do24,22
            # injection_port_0()
            s14_t6 = 0
            s14_io_res_6 = 0
            print("时停40秒")
            while s14_t6 <= 40:
                s14_t6 = s14_t6 + 1
                time.sleep(1)
                s14_io_res_6 = s14_io_res_6 + 1
                if s14_io_res_6 >= 30 and recharge_t == 0:
                    s14_io_res_6 = 0
                    io_restart()  # 复位下dido的问询
                    time.sleep(1)
                if mode == 0 or di_location[0:1][0] == "1":
                    automatic_cleaning_status = 0
                    return
            automatic_cleaning_n = 7

        if mode == 0 or di_location[0:1][0] == "1":
            automatic_cleaning_status = 0
            return

        s14_t7 = 0

        if automatic_cleaning_n == 7:
            print("自动清洗步骤7")
            cut_signal(0)  # 复位DO13
            print("切除信号关闭")
            if do_location[4:5][0] == "0":
                automatic_cleaning_n = 9
            else:
                while 1:
                    s14_t7 = s14_t7 + 1
                    time.sleep(1)
                    if mode == 0 or di_location[0:1][0] == "1":
                        automatic_cleaning_status = 0
                        return
                    if do_location[4:5][0] == "0":
                        automatic_cleaning_n = 9
                        break

        if automatic_cleaning_n == 9:
            print("自动清洗步骤9")
            s14_io_res_9 = 0
            while 1:
                s14_t7 = s14_t7 + 1
                time.sleep(1)
                if mode == 0 or di_location[0:1][0] == "1":
                    automatic_cleaning_status = 0
                    return
                s14_io_res_9 = s14_io_res_9 + 1
                if s14_io_res_9 >= 60 and recharge_t == 0:
                    s14_io_res_9 = 0
                    io_restart()  # 复位下dido的问询
                    time.sleep(1)
                if s14_t7 >= Txh:
                    automatic_cleaning_n = 10
                    break

        if mode == 0 or di_location[0:1][0] == "1":
            automatic_cleaning_status = 0
            return

        if automatic_cleaning_n == 10:
            print("自动清洗步骤10")
            dox_command([11, 20, 13], err_inquiry_s14)  # 输出DO13
            print("切除信号开启")
            time.sleep(3)
            print(do_location)
            if flu == 0:
                automatic_cleaning_n = 12
            elif flu == 1:
                automatic_cleaning_n = 11

        if automatic_cleaning_n == 11:
            print("自动清洗步骤11")
            dox_command([11, 20], err_inquiry_s14)  # 输出DO20
            s14_t11 = 0
            while 1:
                if mode == 0 or di_location[0:1][0] == "1":
                    automatic_cleaning_status = 0
                    return
                s14_t11 = s14_t11 + 1
                time.sleep(1)
                if s14_t11 >= 30:
                    automatic_cleaning_n = 12
                    break

        if mode == 0 or di_location[0:1][0] == "1":
            automatic_cleaning_status = 0
            return

        if automatic_cleaning_n == 12:
            print("自动清洗步骤12")
            injection_port(4)  # 切换阀复位,X-4指令
            time.sleep(5)
            if insert_database() == "ac03":  # 查询指令
                # # serial_val_modify("page0.now_position.txt=\"切换阀X-4位置\"")
                automatic_cleaning_n = 13
                pass  # 成功
            else:
                time.sleep(3)
                if insert_database() == "ac03":  # 查询指令
                    # # serial_val_modify("page0.now_position.txt=\"切换阀X-4位置\"")
                    automatic_cleaning_n = 13
                    pass  # 成功
                else:
                    time1 = time.strftime('%H:%M:%S', time.localtime(time.time()))
                    serial_val_modify("warningpage.data0.insert(\"%s^切换阀故障\")" % time1)  # 存入故障信息
                    # # serial_val_modify("page0.now_position.txt=\"切换阀故障\"")
                    return

            if mode == 0 or di_location[0:1][0] == "1":
                automatic_cleaning_status = 0
                return
            if recharge_t == 0:
                io_restart()
                time.sleep(1)

            if automatic_cleaning_n == 13:
                print("自动清洗步骤13")
                if flu == 0:  # 0为不输出do26
                    dox_command([11, 22], err_inquiry_s14)  # 输出DO22
                    time.sleep(2)
                    automatic_cleaning_n = 14
                elif flu == 1:
                    dox_command([11, 20, 22], err_inquiry_s14)  # 输出DO22
                    time.sleep(2)
                    automatic_cleaning_n = 14

            if automatic_cleaning_n == 14:
                print("自动清洗步骤14")
                if flu == 0:  # 0为不输出do26
                    dox_command([11, 22, 23], err_inquiry_s14)  # 输出DO23
                    s14_14 = 0
                    while s14_14 <= 60:  # 时限一分钟
                        if di_location[5:6][0] == "1" or mode == 0:
                            s14_14 = 70  # 成功后跳出循环
                            automatic_cleaning_n = 16
                            serial_val_modify("homepage.state_sw.val=1")
                            break
                        else:
                            s14_14 = s14_14 + 1  # 没成功时间计数+1
                            time.sleep(1)
                    if s14_14 > 60 and di_location[5:6][0] == "0":
                        time1 = time.strftime('%H:%M:%S', time.localtime(time.time()))
                        serial_val_modify("warningpage.data0.insert(\"%s^检修故障\")" % time1)  # 存入故障信息
                        # # serial_val_modify("page0.now_position.txt=\"检修故障\"")
                        dox_command([3], err_inquiry_s3)  # 输出do3
                        return
                elif flu == 1:
                    dox_command([11, 20, 22, 23], err_inquiry_s14)  # 输出DO23
                    s14_14 = 0
                    while s14_14 <= 60:  # 时限一分钟
                        if di_location[5:6][0] == "1" or mode == 0:
                            s14_14 = 70  # 成功后跳出循环
                            automatic_cleaning_n = 16
                            serial_val_modify("homepage.state_sw.val=1")
                            break
                        else:
                            s14_14 = s14_14 + 1  # 没成功时间计数+1
                            time.sleep(1)
                    if s14_14 > 60 and di_location[5:6][0] == "0":
                        time1 = time.strftime('%H:%M:%S', time.localtime(time.time()))
                        serial_val_modify("warningpage.data0.insert(\"%s^检修故障\")" % time1)  # 存入故障信息
                        # # serial_val_modify("page0.now_position.txt=\"检修故障\"")
                        dox_command([3], err_inquiry_s3)  # 输出do3
                        return

            if automatic_cleaning_n == 16:
                print("自动清洗步骤16")
                dox_command([11, 22], err_inquiry_s14)  # 复位do23,do20
                s14_t15 = 0
                while 1:
                    if mode == 0 or di_location[0:1][0] == "1":
                        automatic_cleaning_status = 0
                        return
                    s14_t15 = s14_t15 + 1
                    time.sleep(1)
                    if s14_t15 >= Tcx:
                        automatic_cleaning_n = 17
                        break

            if mode == 0 or di_location[0:1][0] == "1":
                automatic_cleaning_status = 0
                return
            if recharge_t == 0:
                io_restart()
                time.sleep(1)

            if automatic_cleaning_n == 17:
                print("自动清洗步骤17")
                dox_command([11], err_inquiry_s14)  # 复位do22
                injection_port(1)  # 切换阀复位,X-1指令
                time.sleep(5)
                if insert_database() == "ab02":  # 查询指令
                    # # serial_val_modify("page0.now_position.txt=\"切换阀X-1位置\"")
                    automatic_cleaning_n = 18
                    pass  # 成功
                else:
                    time.sleep(3)
                    if insert_database() == "ab02":  # 查询指令
                        # # serial_val_modify("page0.now_position.txt=\"切换阀X-1位置\"")
                        automatic_cleaning_n = 18
                        pass  # 成功
                    else:
                        time1 = time.strftime('%H:%M:%S', time.localtime(time.time()))
                        serial_val_modify("warningpage.data0.insert(\"%s^切换阀故障\")" % time1)  # 存入故障信息
                        # # serial_val_modify("page0.now_position.txt=\"切换阀故障\"")
                        return

            if automatic_cleaning_n == 18:
                print("自动清洗步骤18")
                dox_command([11, 32], err_inquiry_s14)  # 输出do32
                s14_t18 = 0
                while 1:
                    if mode == 0 or di_location[0:1][0] == "1":
                        automatic_cleaning_status = 0
                        return
                    s14_t18 = s14_t18 + 1
                    time.sleep(1)
                    if s14_t18 >= Tqx1:
                        automatic_cleaning_n = 19
                        break

            if automatic_cleaning_n == 19:
                print("自动清洗步骤19")
                dox_command([11], err_inquiry_s14)  # 复位do32
                s14_t19 = 0
                s14_io_res_19 = 0
                while 1:
                    if mode == 0 or di_location[0:1][0] == "1":
                        automatic_cleaning_status = 0
                        return
                    s14_t19 = s14_t19 + 1
                    time.sleep(1)
                    s14_io_res_19 = s14_io_res_19 + 1
                    if s14_io_res_19 >= 60 and recharge_t == 0:
                        s14_io_res_19 = 0
                        io_restart()  # 复位下dido的问询
                        time.sleep(1)
                    if s14_t19 >= Tqx2:
                        automatic_cleaning_n = 20
                        break

            if automatic_cleaning_n == 20:
                print("自动清洗步骤20")
                injection_port(4)  # 切换阀复位,X-4指令
                time.sleep(5)
                if insert_database() == "ac03":  # 查询指令
                    # # serial_val_modify("page0.now_position.txt=\"切换阀X-4位置\"")
                    automatic_cleaning_n = 21
                    pass  # 成功
                else:
                    time.sleep(3)
                    if insert_database() == "ac03":  # 查询指令
                        # # serial_val_modify("page0.now_position.txt=\"切换阀X-4位置\"")
                        automatic_cleaning_n = 21
                        pass  # 成功
                    else:
                        time1 = time.strftime('%H:%M:%S', time.localtime(time.time()))
                        serial_val_modify("warningpage.data0.insert(\"%s^切换阀故障\")" % time1)  # 存入故障信息
                        # # serial_val_modify("page0.now_position.txt=\"切换阀故障\"")
                        return
            if recharge_t == 0:
                io_restart()
                time.sleep(1)

            if mode == 0 or di_location[0:1][0] == "1":
                automatic_cleaning_status = 0
                return

            if automatic_cleaning_n == 21:
                print("自动清洗步骤21")
                dox_command([11, 22], err_inquiry_s14)  # 输出do22
                s14_t21 = 0
                while 1:
                    if mode == 0 or di_location[0:1][0] == "1":
                        automatic_cleaning_status = 0
                        return
                    s14_t21 = s14_t21 + 1
                    time.sleep(1)
                    if s14_t21 >= Tcx:
                        automatic_cleaning_n = 22
                        break

            if automatic_cleaning_n == 22:
                print("自动清洗步骤22")
                dox_command([11], err_inquiry_s14)  # 复位do22
                automatic_cleaning_n = 23

            if automatic_cleaning_n == 23:
                print("自动清洗步骤23")
                if atcal == 1:
                    automatic_cleaning_n = 24
                    C1 = C1 + 1
                    C1_time = C1 * Txh
                elif atcal == 0:
                    automatic_cleaning_n = 36

            if automatic_cleaning_n == 24:
                print("自动清洗步骤24")
                if C1_time >= Tco and do_location[15:16][0] == "0":
                    print("进入自动校准")
                    serial_val_modify("homepage.now_status.txt=\"自动校准中\"")
                    serial_val_modify("homepage.now_status.txt=\"自动校准中\"")
                    PH_choice(4)  # 选择校准模式
                    time.sleep(3)
                    automatic_cleaning_n = 25
                else:
                    automatic_cleaning_n = 36

            if automatic_cleaning_n == 25:
                print("自动清洗步骤25")
                s14_t25 = 0
                injection_port(2)  # 切换阀复位,X-4指令
                time.sleep(5)
                if insert_database() == "aa01":  # 查询指令
                    pass  # 成功
                else:
                    time.sleep(3)
                    if insert_database() == "aa01":  # 查询指令
                        pass  # 成功
                    else:
                        time1 = time.strftime('%H:%M:%S', time.localtime(time.time()))
                        serial_val_modify("warningpage.data0.insert(\"%s^切换阀故障\")" % time1)  # 存入故障信息
                while 1:
                    if mode == 0 or di_location[0:1][0] == "1":
                        automatic_cleaning_status = 0
                        return
                    s14_t25 = s14_t25 + 1
                    time.sleep(1)
                    if s14_t25 >= 4:
                        automatic_cleaning_n = 26
                        break

            if automatic_cleaning_n == 26:
                print("自动清洗步骤26")
                dox_command([11, 26], err_inquiry_s14)  # 输出do26
                s14_t26 = 0
                while 1:
                    if mode == 0 or di_location[0:1][0] == "1":
                        automatic_cleaning_status = 0
                        return
                    s14_t26 = s14_t26 + 1
                    time.sleep(1)
                    if s14_t26 >= Thc / 2:
                        automatic_cleaning_n = 27
                        break

            if mode == 0 or di_location[0:1][0] == "1":
                automatic_cleaning_status = 0
                return

            if automatic_cleaning_n == 27:
                print("自动清洗步骤27")
                dox_command([11], err_inquiry_s14)  # 复位do26
                s14_t27_1 = 0
                while 1:
                    if mode == 0 or di_location[0:1][0] == "1":
                        automatic_cleaning_status = 0
                        return
                    s14_t27_1 = s14_t27_1 + 1
                    time.sleep(1)
                    if s14_t27_1 >= 29:
                        break
                dox_command([11, 26], err_inquiry_s14)  # 输出do26
                s14_t260 = 0
                while 1:
                    if mode == 0 or di_location[0:1][0] == "1":
                        automatic_cleaning_status = 0
                        return
                    s14_t260 = s14_t260 + 1
                    time.sleep(1)
                    if s14_t260 >= Thc / 2:
                        break
                dox_command([11], err_inquiry_s14)  # 复位do26
                s14_t27 = 0
                while 1:
                    if mode == 0 or di_location[0:1][0] == "1":
                        automatic_cleaning_status = 0
                        return
                    s14_t27 = s14_t27 + 1
                    time.sleep(1)
                    if s14_t27 >= 29:
                        automatic_cleaning_n = 28
                        break

            if automatic_cleaning_n == 28:
                time.sleep(3)
                print("自动清洗步骤28")
                PH_controller(6)
                C2 = C2 + 1
                correction_times_PH_status_6 = 1
                automatic_cleaning_n = 29

            if mode == 0 or di_location[0:1][0] == "1":
                automatic_cleaning_status = 0
                return

            if automatic_cleaning_n == 29:
                print("自动清洗步骤29")
                s14_t29 = 0
                time.sleep(3)
                status_now_6 = PH_status(6)
                if status_now_6 == "66":
                    automatic_cleaning_n = 30
                elif status_now_6 == "11" or status_now_6 == 0:
                    time.sleep(2)
                    while correction_times_PH_status_6 <= 16:
                        status_now_6 = PH_status(6)
                        s14_t29 = 0
                        if status_now_6 == "66":
                            automatic_cleaning_n = 30
                            correction_times_PH_status_6 = 18
                        elif status_now_6 == "11" or status_now_6 == 0:
                            correction_times_PH_status_6 = correction_times_PH_status_6 + 1
                            while 1:
                                if mode == 0 or di_location[0:1][0] == "1":
                                    automatic_cleaning_status = 0
                                    return
                                s14_t29 = s14_t29 + 1
                                time.sleep(1)
                                print("时间计数", s14_t29)
                                if s14_t29 >= 29:
                                    print(status_now_6)
                                    automatic_cleaning_n = 29
                                    break
                        elif PH_status(6) == "FF":
                            time1 = time.strftime('%H:%M:%S', time.localtime(time.time()))
                            serial_val_modify("warningpage.data0.insert(\"%s^校准故障\")" % time1)  # 存入故障信息
                            correction_times_PH_status_6 = 18
                            automatic_cleaning_n = 37
                            break
                    if correction_times_PH_status_6 > 16 and status_now_6 != "66":
                        time1 = time.strftime('%H:%M:%S', time.localtime(time.time()))
                        serial_val_modify("warningpage.data0.insert(\"%s^校准故障\")" % time1)  # 存入故障信息
                        correction_times_PH_status_6 = 18
                        automatic_cleaning_n = 37
                    time.sleep(2)
                elif status_now_6 == "FF":
                    time1 = time.strftime('%H:%M:%S', time.localtime(time.time()))
                    serial_val_modify("warningpage.data0.insert(\"%s^校准故障\")" % time1)  # 存入故障信息
                    correction_times_PH_status_6 = 18
                    automatic_cleaning_n = 37

            if automatic_cleaning_n == 30:
                print("自动清洗步骤30")
                C2 = 0
                injection_port(3)  # 切换阀复位,X-4指令
                time.sleep(5)
                if insert_database() == "af06":  # 查询指令
                    pass  # 成功
                else:
                    time.sleep(3)
                    if insert_database() == "af06":  # 查询指令
                        pass  # 成功
                    else:
                        time1 = time.strftime('%H:%M:%S', time.localtime(time.time()))
                        serial_val_modify("warningpage.data0.insert(\"%s^切换阀故障\")" % time1)  # 存入故障信息
                s14_t30 = 0
                while 1:
                    if mode == 0 or di_location[0:1][0] == "1":
                        automatic_cleaning_status = 0
                        return
                    s14_t30 = s14_t30 + 1
                    time.sleep(1)
                    if s14_t30 >= 5:
                        automatic_cleaning_n = 31
                        break

            if automatic_cleaning_n == 31:
                print("自动清洗步骤31")
                dox_command([11, 28], err_inquiry_s14)  # 输出do28
                s14_t31 = 0
                while 1:
                    if mode == 0 or di_location[0:1][0] == "1":
                        automatic_cleaning_status = 0
                        return
                    s14_t31 = s14_t31 + 1
                    time.sleep(1)
                    if s14_t31 >= Thc / 2:
                        automatic_cleaning_n = 32
                        break

            if automatic_cleaning_n == 32:
                print("自动清洗步骤32")
                dox_command([11], err_inquiry_s14)  # 复位do28
                s14_t32_1 = 0
                while 1:
                    if mode == 0 or di_location[0:1][0] == "1":
                        automatic_cleaning_status = 0
                        return
                    s14_t32_1 = s14_t32_1 + 1
                    time.sleep(1)
                    if s14_t32_1 >= 29:
                        break
                dox_command([11, 28], err_inquiry_s14)  # 输出do28
                s14_t310 = 0
                while 1:
                    if mode == 0 or di_location[0:1][0] == "1":
                        automatic_cleaning_status = 0
                        return
                    s14_t310 = s14_t310 + 1
                    time.sleep(1)
                    if s14_t310 >= Thc / 2:
                        break
                dox_command([11], err_inquiry_s14)  # 复位do24
                s14_t32 = 0
                while 1:
                    if mode == 0 or di_location[0:1][0] == "1":
                        automatic_cleaning_status = 0
                        return
                    s14_t32 = s14_t32 + 1
                    time.sleep(1)
                    if s14_t32 >= 29:
                        automatic_cleaning_n = 33
                        break

            if automatic_cleaning_n == 33:
                print("自动清洗步骤33")
                time.sleep(3)
                PH_controller(4)
                C2 = C2 + 1
                correction_times_PH_status_4 = 1
                automatic_cleaning_n = 34

            if mode == 0 or di_location[0:1][0] == "1":
                automatic_cleaning_status = 0
                return

            if automatic_cleaning_n == 34:
                s14_t34 = 0
                print("自动清洗步骤34")
                time.sleep(3)
                status_now_4 = PH_status(4)
                if status_now_4 == "44":
                    automatic_cleaning_n = 35
                elif status_now_4 == "11" or status_now_4 == 0:
                    time.sleep(2)
                    while correction_times_PH_status_4 <= 16:
                        status_now_4 = PH_status(4)
                        s14_t34 = 0
                        if status_now_4 == "44":
                            automatic_cleaning_n = 35
                            correction_times_PH_status_4 = 18
                        elif status_now_4 == "11" or status_now_6 == 0:
                            correction_times_PH_status_4 = correction_times_PH_status_4 + 1
                            while 1:
                                if mode == 0 or di_location[0:1][0] == "1":
                                    automatic_cleaning_status = 0
                                    return
                                s14_t34 = s14_t34 + 1
                                time.sleep(1)
                                print("时间计数", s14_t34)
                                if s14_t34 >= 29:
                                    automatic_cleaning_n = 34
                                    print(status_now_4)
                                    break
                        elif status_now_4 == "FF":
                            time1 = time.strftime('%H:%M:%S', time.localtime(time.time()))
                            serial_val_modify("warningpage.data0.insert(\"%s^校准故障\")" % time1)  # 存入故障信息
                            correction_times_PH_status_4 = 18
                            automatic_cleaning_n = 37
                    if correction_times_PH_status_4 >= 16 and status_now_4 != "44":
                        time1 = time.strftime('%H:%M:%S', time.localtime(time.time()))
                        serial_val_modify("warningpage.data0.insert(\"%s^校准故障\")" % time1)  # 存入故障信息
                        correction_times_PH_status_4 = 18
                        automatic_cleaning_n = 37
                elif status_now_4 == "FF":
                    time1 = time.strftime('%H:%M:%S', time.localtime(time.time()))
                    serial_val_modify("warningpage.data0.insert(\"%s^校准故障\")" % time1)  # 存入故障信息
                    automatic_cleaning_n = 37

            if automatic_cleaning_n == 35:
                print("自动清洗步骤35")
                C1 = 0
                C2 = 0
                PH_choice(0)
                time.sleep(3)  # 退出校准模式
                automatic_cleaning_n = 36

            if automatic_cleaning_n == 36:
                print("自动清洗步骤36")
                serial_val_modify("homepage.now_status.txt=\"自动清洗中\"")
                serial_val_modify("homepage.now_status.txt=\"自动清洗中\"")
                automatic_cleaning_n = 2

            if automatic_cleaning_n == 37:
                print("自动清洗步骤37")
                # injection_port_0()
                dox_command([0], err_inquiry_s14)  # 复位所有
                PH_choice(0)
                time.sleep(3)  # 退出校准模式
                while 1:
                    time.sleep(1)
                    if mode == 0 or di_location[0:1][0] == "1":
                        automatic_cleaning_status = 0
                        return


def remote_maintenance_s15():
    global serial_txt, mode, di_location, remote_maintenance_status
    global flu, atcal, do_location, err_inquiry_s15, recharge_t
    remote_maintenance_n = 0
    while 1:
        if remote_maintenance_n == 0:
            s15_t0 = 0  # 计时使用
            while s15_t0 <= 50:  # 时限5秒内可返回手动
                if mode == 0 or di_location[0:1][0] == "0":
                    remote_maintenance_status = 0
                    return
                s15_t0 = s15_t0 + 1  # 没成功时间计数+1
                time.sleep(0.1)
            remote_maintenance_n = 1

        if remote_maintenance_n == 1:
            print("远程保养步骤1")
            serial_val_modify("homepage.now_status.txt=\"保养中\"")
            serial_val_modify("homepage.now_status.txt=\"保养中\"")
            injection_port(1)  # 切换阀复位,X-1指令
            time.sleep(5)
            if insert_database() == "ab02":  # 查询指令
                # # serial_val_modify("page0.now_position.txt=\"切换阀X-1位置\"")
                pass  # 成功
            else:
                time.sleep(3)
                if insert_database() == "ab02":  # 查询指令
                    # # serial_val_modify("page0.now_position.txt=\"切换阀X-1位置\"")
                    pass  # 成功
                else:
                    time1 = time.strftime('%H:%M:%S', time.localtime(time.time()))
                    serial_val_modify("warningpage.data0.insert(\"%s^切换阀故障\")" % time1)  # 存入故障信息
                    # # serial_val_modify("page0.now_position.txt=\"切换阀故障\"")
                    return
            dox_command([11, 13], err_inquiry_s15)
            remote_maintenance_n = 2

        if mode == 0 or di_location[0:1][0] == "0":
            remote_maintenance_status = 0
            return

        if remote_maintenance_n == 2:
            print("远程保养步骤2")
            if flu == 0:
                remote_maintenance_n = 4
            elif flu == 1:
                remote_maintenance_n = 3

        if remote_maintenance_n == 3:
            print("远程保养步骤3")
            dox_command([11, 13, 20], err_inquiry_s15)
            s15_t3 = 0  # 计时使用
            while s15_t3 <= 30:  # 时停30秒
                if mode == 0 or di_location[0:1][0] == "0":
                    remote_maintenance_status = 0
                    return
                s15_t3 = s15_t3 + 1  # 没成功时间计数+1
                time.sleep(1)
            remote_maintenance_n = 4

        if remote_maintenance_n == 4:
            print("远程保养步骤4")
            injection_port(4)  # 切换阀复位,X-4指令
            time.sleep(5)
            if insert_database() == "ac03":  # 查询指令
                # # serial_val_modify("page0.now_position.txt=\"切换阀X-4位置\"")
                pass  # 成功
            else:
                time.sleep(3)
                if insert_database() == "ac03":  # 查询指令
                    # # serial_val_modify("page0.now_position.txt=\"切换阀X-4位置\"")
                    pass  # 成功
                else:
                    time1 = time.strftime('%H:%M:%S', time.localtime(time.time()))
                    serial_val_modify("warningpage.data0.insert(\"%s^切换阀故障\")" % time1)  # 存入故障信息
                    # # serial_val_modify("page0.now_position.txt=\"切换阀故障\"")
                    return
            remote_maintenance_n = 5

        if mode == 0 or di_location[0:1][0] == "0":
            remote_maintenance_status = 0
            return
        if recharge_t == 0:
            io_restart()
            time.sleep(1)

        if remote_maintenance_n == 5:
            print("远程保养步骤5")
            if flu == 0:
                dox_command([11, 13, 22], err_inquiry_s15)
                time.sleep(2)
                remote_maintenance_n = 6
            elif flu == 1:
                dox_command([11, 13, 20, 22], err_inquiry_s15)
                time.sleep(2)
                remote_maintenance_n = 6

        if remote_maintenance_n == 6:
            print("远程保养步骤6")
            if flu == 0:
                dox_command([11, 13, 22, 23], err_inquiry_s15)
                s15_16 = 0
                while s15_16 <= 60:  # 时限一分钟
                    if di_location[5:6][0] == "1" or mode == 0:
                        s15_16 = 70  # 成功后跳出循环
                        remote_maintenance_n = 8
                        serial_val_modify("homepage.state_sw.val=1")
                        break
                    else:
                        s15_16 = s15_16 + 1  # 没成功时间计数+1
                        time.sleep(1)
                if s15_16 > 60 and di_location[5:6][0] == "0":
                    time1 = time.strftime('%H:%M:%S', time.localtime(time.time()))
                    serial_val_modify("warningpage.data0.insert(\"%s^检修故障\")" % time1)  # 存入故障信息
                    # # serial_val_modify("page0.now_position.txt=\"检修故障\"")
                    dox_command([12], err_inquiry_s3)  # 输出do3
                    return
            elif flu == 1:
                dox_command([11, 13, 22, 20, 23], err_inquiry_s15)
                s15_16 = 0
                while s15_16 <= 60:  # 时限一分钟
                    if di_location[5:6][0] == "1" or mode == 0:
                        s15_16 = 70  # 成功后跳出循环
                        remote_maintenance_n = 8
                        serial_val_modify("homepage.state_sw.val=1")
                        break
                    else:
                        s15_16 = s15_16 + 1  # 没成功时间计数+1
                        time.sleep(1)
                if s15_16 > 60 and di_location[5:6][0] == "0":
                    time1 = time.strftime('%H:%M:%S', time.localtime(time.time()))
                    serial_val_modify("warningpage.data0.insert(\"%s^检修故障\")" % time1)  # 存入故障信息
                    # # serial_val_modify("page0.now_position.txt=\"检修故障\"")
                    dox_command([12], err_inquiry_s3)  # 输出do3
                    return

        if mode == 0 or di_location[0:1][0] == "0":
            remote_maintenance_status = 0
            return

        if remote_maintenance_n == 8:
            print("远程保养步骤8")
            dox_command([11, 13, 22], err_inquiry_s15)
            s15_t8 = 0  # 计时使用
            while s15_t8 <= Tcx:
                if mode == 0 or di_location[0:1][0] == "0":
                    remote_maintenance_status = 0
                    return
                s15_t8 = s15_t8 + 1  # 没成功时间计数+1
                time.sleep(1)
            remote_maintenance_n = 9

        if recharge_t == 0:
            io_restart()
            time.sleep(1)

        if remote_maintenance_n == 9:
            print("远程保养步骤9")
            dox_command([11, 13], err_inquiry_s15)
            injection_port(1)  # 切换阀复位,X-1指令
            time.sleep(5)
            if insert_database() == "ab02":  # 查询指令
                # # serial_val_modify("page0.now_position.txt=\"切换阀X-1位置\"")
                pass  # 成功
            else:
                time.sleep(3)
                if insert_database() == "ab02":  # 查询指令
                    # # serial_val_modify("page0.now_position.txt=\"切换阀X-1位置\"")
                    pass  # 成功
                else:
                    time1 = time.strftime('%H:%M:%S', time.localtime(time.time()))
                    serial_val_modify("warningpage.data0.insert(\"%s^切换阀故障\")" % time1)  # 存入故障信息
                    # # serial_val_modify("page0.now_position.txt=\"切换阀故障\"")
                    return
            remote_maintenance_n = 10

        if mode == 0 or di_location[0:1][0] == "0":
            remote_maintenance_status = 0
            return

        if remote_maintenance_n == 10:
            print("远程保养步骤10")
            dox_command([11, 13, 32], err_inquiry_s15)
            s15_t10 = 0  # 计时使用
            while s15_t10 <= Tqx1:
                if mode == 0 or di_location[0:1][0] == "0":
                    remote_maintenance_status = 0
                    return
                s15_t10 = s15_t10 + 1  # 没成功时间计数+1
                time.sleep(1)
            remote_maintenance_n = 11

        if remote_maintenance_n == 11:
            print("远程保养步骤11")
            dox_command([11, 13], err_inquiry_s15)
            s15_t11 = 0  # 计时使用
            s15_io_res_11 = 0
            while s15_t11 <= Tqx2:
                s15_io_res_11 = s15_io_res_11 + 1
                if s15_io_res_11 >= 60 and recharge_t == 0:
                    s15_io_res_11 = 0
                    io_restart()  # 复位下dido的问询
                if mode == 0 or di_location[0:1][0] == "0":
                    remote_maintenance_status = 0
                    return
                s15_t11 = s15_t11 + 1  # 没成功时间计数+1
                time.sleep(1)
            remote_maintenance_n = 12

        if mode == 0 or di_location[0:1][0] == "0":
            remote_maintenance_status = 0
            return

        if remote_maintenance_n == 12:
            print("远程保养步骤12")
            dox_command([11, 13, 30], err_inquiry_s15)
            s15_t12 = 0  # 计时使用
            s15_io_res_12 = 0
            while s15_t12 <= Tbh:
                if mode == 0 or di_location[0:1][0] == "0":
                    remote_maintenance_status = 0
                    return
                s15_io_res_12 = s15_io_res_12 + 1
                if s15_io_res_12 >= 60 and recharge_t == 0:
                    s15_io_res_12 = 0
                    io_restart()  # 复位下dido的问询
                s15_t12 = s15_t12 + 1  # 没成功时间计数+1
                time.sleep(1)
            remote_maintenance_n = 13

        if remote_maintenance_n == 13:
            print("远程保养步骤13")
            dox_command([11, 13], err_inquiry_s15)
            # injection_port_0()
            remote_maintenance_n = 14

        if mode == 0 or di_location[0:1][0] == "0":
            remote_maintenance_status = 0
            return

        if remote_maintenance_n == 14:
            print("远程保养步骤14")
            s15_t14 = 0  # 计时使用
            s15_io_res_14 = 0
            while s15_t14 <= Tby:
                if mode == 0 or di_location[0:1][0] == "0":
                    remote_maintenance_status = 0
                    return
                s15_io_res_14 = s15_io_res_14 + 1
                if s15_io_res_14 >= 60 and recharge_t == 0:
                    s15_io_res_14 = 0
                    io_restart()  # 复位下dido的问询
                s15_t14 = s15_t14 + 1  # 没成功时间计数+1
                time.sleep(1)
            remote_maintenance_n = 12


def cut_signal(num):
    global err_inquiry_s14_13
    if num == 1:
        dox_command([13], err_inquiry_s14_13)
    elif num == 0:
        dox_command([0], err_inquiry_s14_13)


def control():  # 主控制程序，用于手动，自动保养和自动清洗的切换
    global mode, manual_status, remote_maintenance_status, automatic_cleaning_status
    global do_location, di_location
    manual_status = 0  # 手动模式开启记录
    remote_maintenance_status = 0  # 自动保养开启记录
    automatic_cleaning_status = 0  # 自动清洗开启记录
    err_status = 0
    while 1:
        if mode == 0 and manual_status == 0:
            manual_status = 1
            err_status = 0
            print("手动")
            serial_val_modify("homepage.now_status.txt=\"设备开启完成\"")
            manual_control()

        if mode == 1 and di_location[0:1][0] == "0" and automatic_cleaning_status == 0 \
                and do_location[11:12][0] == "0" and do_location[13:14][0] == "0" and do_location[14:15][0] == "0":
            automatic_cleaning_status = 1
            print("自动清洗")
            serial_val_modify("homepage.now_status.txt=\"设备开启完成\"")
            automatic_cleaning_s14()
            serial_val_modify("homepage.now_status.txt=\"重启中\"")
            #  # serial_val_modify("page0.now_status.txt=\"重启中\"")
            restart_exe()

        if mode == 1 and di_location[0:1][0] == "1" and remote_maintenance_status == 0 \
                and do_location[11:12][0] == "0" and do_location[13:14][0] == "0" and do_location[14:15][0] == "0":
            remote_maintenance_status = 1
            print("自动保养")
            serial_val_modify("homepage.now_status.txt=\"设备开启完成\"")
            remote_maintenance_s15()
            serial_val_modify("homepage.now_status.txt=\"重启中\"")
            #  # serial_val_modify("page0.now_status.txt=\"重启中\"")
            restart_exe()

        if mode == 1 and do_location[11:12][0] == "1" and err_status == 0:
            err_status = 1
            serial_val_modify("homepage.now_status.txt=\"设备故障\"")

        if (mode == 1 and do_location[13:14][0] == "1" and err_status) == 0 or \
                (mode == 1 and do_location[14:15][0] == "1" and err_status == 0):
            err_status = 1
            serial_val_modify("homepage.now_status.txt=\"液体不足\"")


def sql_insert_parameter(tqx1, tqx2, tby, txh, tco, thc):  # 储存当前系统参数
    con = pymysql.connect(
        host='127.0.0.1',
        port=3306,
        user='root',
        password='!qAz2WsX3edc',
        db='ph',
        charset='utf8'
    )
    cur = con.cursor()
    time1 = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
    cur.executemany("INSERT system_parameter(date,tqx1,tqx2,tby,txh,tco,thc) VALUE(%s,%s,%s,%s,%s,%s,%s)",
                    [(time1, tqx1, tqx2, tby, txh, tco, thc)])

    cur.execute("SELECT min(id) FROM system_parameter ")
    min_mode = int(cur.fetchone()[0])
    cur.execute("SELECT max(id) FROM system_parameter ")
    max_mode = int(cur.fetchone()[0])
    if (max_mode - min_mode) >= 1000:
        sql2 = ("delete FROM system_parameter " + "WHERE id <= %s " % (max_mode - 100) + " and id >= %s" % min_mode)
        cur.execute(sql2)
    con.commit()
    cur.close()
    return


def sql_system_parameter():  # 查询当前系统参数
    global Tqx1, Tqx2, Tby, Txh, Tco, Thc
    con = pymysql.connect(
        host='127.0.0.1',
        port=3306,
        user='root',
        password='!qAz2WsX3edc',
        db='ph',
        charset='utf8'
    )
    cur = con.cursor()
    cur.execute("select tqx1 from system_parameter order by id desc limit 1")
    Tqx1 = int(cur.fetchone()[0])
    cur.execute("select tqx2 from system_parameter order by id desc limit 1")
    Tqx2 = int(cur.fetchone()[0])
    cur.execute("select tby from system_parameter order by id desc limit 1")
    Tby = int(cur.fetchone()[0])
    cur.execute("select txh from system_parameter order by id desc limit 1")
    Txh = int(cur.fetchone()[0])
    cur.execute("select tco from system_parameter order by id desc limit 1")
    Tco = int(cur.fetchone()[0])
    cur.execute("select thc from system_parameter order by id desc limit 1")
    Thc = int(cur.fetchone()[0])
    cur.close()
    return


def system_parameter():
    global Tqx1, Tqx2, Tby, Txh, serial_txt, Tco, Thc
    global serial_time, serial_txt
    system_parameter_count = 0
    year = time.strftime("%Y")
    month = time.strftime("%m")
    day = time.strftime("%d")
    hour = time.strftime("%H")
    minute = time.strftime("%M")
    while 1:
        time.sleep(0.2)
        if system_parameter_count == 0:
            system_parameter_count = 1
            sql_system_parameter()
            time.sleep(1)
            clean_time = int(Txh / 3600)
            lnjection_time = int(Tqx1)
            teardown_time = int(Tby / 3600)
            soaking_time = int(Tqx2 / 60)
            cavity_time = int(Tco / 3600)
            corr_h2o_time = int(Thc)
            serial_val_modify("runpage.online_val.val=%s" % clean_time)
            serial_val_modify("runpage.pump_4_val.val=%s" % lnjection_time)
            serial_val_modify("runpage.pro_pump_val.val=%s" % teardown_time)
            serial_val_modify("runpage.pole_pump_val.val=%s" % soaking_time)
            serial_val_modify("runpage.cavity_val.val=%s" % cavity_time)
            serial_val_modify("runpage.corr_h2o_time.val=%s" % corr_h2o_time)
            print("成功")

        if str(serial_txt)[0:4] == "0201":
            Txh_s = str(serial_txt[4:6])  # 自动清洗间隔
            Txh = (int(Txh_s, 16)) * 3600
            serial_txt = 0
            sql_insert_parameter(Tqx1, Tqx2, Tby, Txh, Tco, Thc)

        if str(serial_txt)[0:4] == "0202":
            Tco_s = str(serial_txt[4:6])  # 自动校准间隔
            Tco = (int(Tco_s, 16)) * 3600
            serial_txt = 0
            sql_insert_parameter(Tqx1, Tqx2, Tby, Txh, Tco, Thc)

        if str(serial_txt)[0:4] == "0203":
            Tby_s = str(serial_txt[4:6])  # 保护液输送间隔
            Tby = (int(Tby_s, 16)) * 3600
            serial_txt = 0
            sql_insert_parameter(Tqx1, Tqx2, Tby, Txh, Tco, Thc)

        if str(serial_txt)[0:4] == "0204":
            Tqx2_s = str(serial_txt[4:6])  # 电极浸泡时间
            Tqx2 = (int(Tqx2_s, 16)) * 60
            serial_txt = 0
            sql_insert_parameter(Tqx1, Tqx2, Tby, Txh, Tco, Thc)

        if str(serial_txt)[0:4] == "0205":
            Tqx1_s = str(serial_txt[4:6])  # 清洗液输送时间
            Tqx1 = (int(Tqx1_s, 16))
            serial_txt = 0
            sql_insert_parameter(Tqx1, Tqx2, Tby, Txh, Tco, Thc)

        if str(serial_txt)[0:4] == "0206":
            Thc_s = str(serial_txt[4:6])  # 缓冲液输送时间
            Thc = (int(Thc_s, 16))
            serial_txt = 0
            sql_insert_parameter(Tqx1, Tqx2, Tby, Txh, Tco, Thc)

        if str(serial_time)[0:4] == "0101":
            year_s = serial_time[4:8]  # 年
            year = (int(year_s, 16))
            serial_time = 0

        if str(serial_time)[0:4] == "0102":
            month_s = serial_time[4:6]  # 月
            month = (int(month_s, 16))
            serial_time = 0

        if str(serial_time)[0:4] == "0103":
            day_s = serial_time[4:6]  # 日
            day = (int(day_s, 16))
            serial_time = 0

        if str(serial_time)[0:4] == "0104":
            hour_s = serial_time[4:6]  # 小时
            hour = (int(hour_s, 16))
            serial_time = 0

        if str(serial_time)[0:4] == "0105":
            minute_s = serial_time[4:6]  # 分钟
            minute = (int(minute_s, 16))
            serial_time = 0

        if str(serial_time)[0:4] == "0106":
            time_date = str(year) + "-" + str(month) + "-" + str(day) + " " + str(hour) + ":" + str(minute) + ":00"
            time_update = "sudo date  --s=" + time_date
            print(time_update)
            serial_time = 0
            os.system(time_update)

        if str(serial_txt)[0:4] == "d8d8":
            serial_val_modify("homepage.now_status.txt=\"重启中\"")
            #  # serial_val_modify("page0.now_status.txt=\"重启中\"")
            restart_exe()


def recharge():  # 补充液体
    global serial_txt, recharge_t, status_restart_timing
    while 1:
        time.sleep(0.2)
        if serial_txt == "080101" and status_restart_timing == 0:
            recharge_t = 1
            replenish_clean_supply_s10()  # 补充清洗液
            recharge_t = 0
            serial_txt = 0

        if serial_txt == "080301" and status_restart_timing == 0:
            recharge_t = 1
            replenish_protect_supply_s11()  # 补充保护液
            recharge_t = 0
            serial_txt = 0

        if serial_txt == "080201" and status_restart_timing == 0:
            recharge_t = 1
            replenish_rehydration_4_s12()  # 补充缓冲液4
            recharge_t = 0
            serial_txt = 0

        if serial_txt == "080401" and status_restart_timing == 0:
            recharge_t = 1
            replenish_rehydration_6_s13()  # 补充缓冲液6
            recharge_t = 0
            serial_txt = 0


threads = []
t1 = threading.Thread(target=serial_read)
threads.append(t1)

t2 = threading.Thread(target=recharge)
threads.append(t2)

t3 = threading.Thread(target=control)
threads.append(t3)

t4 = threading.Thread(target=water_level_monitoring)
threads.append(t4)

t5 = threading.Thread(target=DHT11_start)
threads.append(t5)

t6 = threading.Thread(target=system_parameter)
threads.append(t6)

t7 = threading.Thread(target=io_status)
threads.append(t7)

t8 = threading.Thread(target=do_not_reset)
threads.append(t8)

if __name__ == "__main__":
    # 明日还需设置时间，建立时间表
    # 程序状态
    s4_state, s5_state, s6_state, s7_state, s8_state = [0], [0], [0], [0], [0]
    s9_state, s10_state, s11_state, s12_state, s13_state = [0], [0], [0], [0], [0]
    s1_state, s2_state = [0], [0]
    s2_state_13 = [0]
    DHT11_state_1, DHT11_state_2, DHT11_state_3, DHT11_state_4, DHT11_state_21 = [0], [0], [0], [0], [0]
    recharge_t = 0  # 用于补充液体时，can通讯不复位，不复位那部分暂时还没写
    i2c40, i2c41, i2c42, i2c43 = 0, 0, 0, 0  # 初始化四个液位
    i2c40_t, i2c41_t, i2c42_t, i2c43_t = 0, 0, 0, 0  # 低液位报警，报警时为1
    YW40L = 60  # 清洗液最低水位
    YW41L = 60  # 保护液最低水位
    YW42L = 60  # 缓冲液4最低水位
    YW43L = 60  # 缓冲液6最低水位
    YW40H = 200  # 清洗液最高水位
    YW41H = 200  # 保护液最高水位
    YW42H = 200  # 缓冲液4最高水位
    YW43H = 200  # 缓冲液6最高水位
    err_inquiry_s1 = ['0']  # s1所用的不可复位do
    err_inquiry_s2 = ['0']  # s2所用的不可复位do
    err_inquiry_s3 = ['0']  # s3所用的不可复位do
    err_inquiry_s3_exit = ['0']  # s3退出所用的不可复位do
    err_inquiry_s4_s10 = ['0']  # s4到s10所用的不可复位do
    err_inquiry_s10_s13 = ['0']  # s10到s13所用的不可复位do
    err_inquiry_s14_13 = ['0']
    err_inquiry_s01 = ['0']  # 用于手动状态下的首次复位
    err_inquiry_s14 = ['0']  # 用于自动动状态下的首次复位,还有s14
    err_inquiry_s15 = ['0']  # s15
    err_inquiry_14 = ['0']  # 清洗液报警专用
    err_inquiry_15 = ['0']  # 保护液报警专用
    err_inquiry_16 = ['0']  # 缓冲液报警专用
    err_inquiry_21 = ['0']  # 温湿度报警专用
    err_inquiry_1_4 = ['0']  # 温湿度报警专用
    err_inquiry_27_30 = ['0']  # 补液专用
    manual_status = 0  # 手动模式开启记录
    remote_maintenance_status = 0  # 自动保养开启记录
    automatic_cleaning_status = 0  # 自动清洗开启记录
    do_location = ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
                   '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
    di_location = ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
                   '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
    # do和di状态的首次状态预置值
    serial_txt = 0  # 串口所读文本
    serial_time = 0  # 标定时间所用
    mode = 3  # 3是没有选择模式，0是手动模式，1是自动模式
    data_do, data_di = 0, 0  # 用于判断do和di
    var1, var2 = '00000000000000000000000000000000', '00000000000000000000000000000000'  # 用于判断do和di
    status_restart_timing = 1  # 用于重启can通讯的预置值，1为开启重启
    io_status_quit = 1  # 用于重启can通讯的预置值，1为开启重启
    flu = 1  # 在线冲洗，0为没有，1为有
    atcal = 1  # 自动校准，0为没有，1为有
    Tcx = 25  # 水冲洗时间，单位：秒
    Tqx1 = 5  # 清洗液输送时间，单位：秒
    Tqx2 = 60  # 清洗液浸泡时间，5分钟,换算秒为300秒
    Tbh = 8  # 保护液输送时间，单位：秒
    Tby = 1296000  # 再次保养间隔时间，15天，换算秒为1296000秒
    Thc = 15  # 缓冲液输送时间，单位：秒
    Txh = 3600  # 清洗液大循环时间，36小时，换算秒为129600秒
    Tco = 1296000  # 自动校准间隔
    can_open()
    serial_val_modify("homepage.now_status.txt=\"开启中\"")
    query_mode()  # 查询当前所处的模式，并在触摸屏中显示
    time.sleep(1)
    while 1:
        for t in threads:
            t.start()
        for t in threads:
            t.join()
